blob: f9b51d8b1233602223db9db3c249149a7ef28e22 [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;
712 struct tm *tm;
713 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
729 tm = localtime((time_t *)&s->logs.tv_accept.tv_sec);
730
731
732 /* FIXME: let's limit ourselves to frontend logging for now. */
733 tolog = fe->to_log;
734
735 h = tmpline;
736 if (fe->to_log & LW_REQHDR &&
737 txn->req.cap &&
738 (h < tmpline + sizeof(tmpline) - 10)) {
739 *(h++) = ' ';
740 *(h++) = '{';
741 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
742 if (hdr)
743 *(h++) = '|';
744 if (txn->req.cap[hdr] != NULL)
745 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
746 '#', hdr_encode_map, txn->req.cap[hdr]);
747 }
748 *(h++) = '}';
749 }
750
751 if (fe->to_log & LW_RSPHDR &&
752 txn->rsp.cap &&
753 (h < tmpline + sizeof(tmpline) - 7)) {
754 *(h++) = ' ';
755 *(h++) = '{';
756 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
757 if (hdr)
758 *(h++) = '|';
759 if (txn->rsp.cap[hdr] != NULL)
760 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
761 '#', hdr_encode_map, txn->rsp.cap[hdr]);
762 }
763 *(h++) = '}';
764 }
765
766 if (h < tmpline + sizeof(tmpline) - 4) {
767 *(h++) = ' ';
768 *(h++) = '"';
769 uri = txn->uri ? txn->uri : "<BADREQ>";
770 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
771 '#', url_encode_map, uri);
772 *(h++) = '"';
773 }
774 *h = '\0';
775
776 svid = (tolog & LW_SVID) ?
777 (s->data_source != DATA_SRC_STATS) ?
778 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
779
780 send_log(prx_log, LOG_INFO,
781 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
782 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
783 " %s %s %c%c%c%c %d/%d/%d/%d %d/%d%s\n",
784 pn,
785 (s->cli_addr.ss_family == AF_INET) ?
786 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
787 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
788 tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900,
789 tm->tm_hour, tm->tm_min, tm->tm_sec, s->logs.tv_accept.tv_usec/1000,
790 fe->id, be->id, svid,
791 s->logs.t_request,
792 (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1,
793 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
794 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
795 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
796 txn->status,
797 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in,
798 txn->cli_cookie ? txn->cli_cookie : "-",
799 txn->srv_cookie ? txn->srv_cookie : "-",
800 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
801 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
802 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
803 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
804 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
805 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
806
807 s->logs.logwait = 0;
808}
809
Willy Tarreau117f59e2007-03-04 18:17:17 +0100810
811/*
812 * Capture headers from message starting at <som> according to header list
813 * <cap_hdr>, and fill the <idx> structure appropriately.
814 */
815void capture_headers(char *som, struct hdr_idx *idx,
816 char **cap, struct cap_hdr *cap_hdr)
817{
818 char *eol, *sol, *col, *sov;
819 int cur_idx;
820 struct cap_hdr *h;
821 int len;
822
823 sol = som + hdr_idx_first_pos(idx);
824 cur_idx = hdr_idx_first_idx(idx);
825
826 while (cur_idx) {
827 eol = sol + idx->v[cur_idx].len;
828
829 col = sol;
830 while (col < eol && *col != ':')
831 col++;
832
833 sov = col + 1;
834 while (sov < eol && http_is_lws[(unsigned char)*sov])
835 sov++;
836
837 for (h = cap_hdr; h; h = h->next) {
838 if ((h->namelen == col - sol) &&
839 (strncasecmp(sol, h->name, h->namelen) == 0)) {
840 if (cap[h->index] == NULL)
841 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200842 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100843
844 if (cap[h->index] == NULL) {
845 Alert("HTTP capture : out of memory.\n");
846 continue;
847 }
848
849 len = eol - sov;
850 if (len > h->len)
851 len = h->len;
852
853 memcpy(cap[h->index], sov, len);
854 cap[h->index][len]=0;
855 }
856 }
857 sol = eol + idx->v[cur_idx].cr + 1;
858 cur_idx = idx->v[cur_idx].next;
859 }
860}
861
862
Willy Tarreau42250582007-04-01 01:30:43 +0200863/* either we find an LF at <ptr> or we jump to <bad>.
864 */
865#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
866
867/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
868 * otherwise to <http_msg_ood> with <state> set to <st>.
869 */
870#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
871 ptr++; \
872 if (likely(ptr < end)) \
873 goto good; \
874 else { \
875 state = (st); \
876 goto http_msg_ood; \
877 } \
878 } while (0)
879
880
Willy Tarreaubaaee002006-06-26 02:48:02 +0200881/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100882 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100883 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
884 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
885 * will give undefined results.
886 * Note that it is upon the caller's responsibility to ensure that ptr < end,
887 * and that msg->sol points to the beginning of the response.
888 * If a complete line is found (which implies that at least one CR or LF is
889 * found before <end>, the updated <ptr> is returned, otherwise NULL is
890 * returned indicating an incomplete line (which does not mean that parts have
891 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
892 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
893 * upon next call.
894 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200895 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100896 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
897 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200898 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100899 */
Willy Tarreaua15645d2007-03-18 16:22:39 +0100900const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100901 const char *ptr, const char *end,
902 char **ret_ptr, int *ret_state)
903{
904 __label__
905 http_msg_rpver,
906 http_msg_rpver_sp,
907 http_msg_rpcode,
908 http_msg_rpcode_sp,
909 http_msg_rpreason,
910 http_msg_rpline_eol,
911 http_msg_ood, /* out of data */
912 http_msg_invalid;
913
914 switch (state) {
915 http_msg_rpver:
916 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100917 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100918 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
919
920 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100921 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100922 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
923 }
924 goto http_msg_invalid;
925
926 http_msg_rpver_sp:
927 case HTTP_MSG_RPVER_SP:
928 if (likely(!HTTP_IS_LWS(*ptr))) {
929 msg->sl.st.c = ptr - msg_buf;
930 goto http_msg_rpcode;
931 }
932 if (likely(HTTP_IS_SPHT(*ptr)))
933 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
934 /* so it's a CR/LF, this is invalid */
935 goto http_msg_invalid;
936
937 http_msg_rpcode:
938 case HTTP_MSG_RPCODE:
939 if (likely(!HTTP_IS_LWS(*ptr)))
940 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
941
942 if (likely(HTTP_IS_SPHT(*ptr))) {
943 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
944 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
945 }
946
947 /* so it's a CR/LF, so there is no reason phrase */
948 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
949 http_msg_rsp_reason:
950 /* FIXME: should we support HTTP responses without any reason phrase ? */
951 msg->sl.st.r = ptr - msg_buf;
952 msg->sl.st.r_l = 0;
953 goto http_msg_rpline_eol;
954
955 http_msg_rpcode_sp:
956 case HTTP_MSG_RPCODE_SP:
957 if (likely(!HTTP_IS_LWS(*ptr))) {
958 msg->sl.st.r = ptr - msg_buf;
959 goto http_msg_rpreason;
960 }
961 if (likely(HTTP_IS_SPHT(*ptr)))
962 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
963 /* so it's a CR/LF, so there is no reason phrase */
964 goto http_msg_rsp_reason;
965
966 http_msg_rpreason:
967 case HTTP_MSG_RPREASON:
968 if (likely(!HTTP_IS_CRLF(*ptr)))
969 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
970 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
971 http_msg_rpline_eol:
972 /* We have seen the end of line. Note that we do not
973 * necessarily have the \n yet, but at least we know that we
974 * have EITHER \r OR \n, otherwise the response would not be
975 * complete. We can then record the response length and return
976 * to the caller which will be able to register it.
977 */
978 msg->sl.st.l = ptr - msg->sol;
979 return ptr;
980
981#ifdef DEBUG_FULL
982 default:
983 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
984 exit(1);
985#endif
986 }
987
988 http_msg_ood:
989 /* out of data */
990 if (ret_state)
991 *ret_state = state;
992 if (ret_ptr)
993 *ret_ptr = (char *)ptr;
994 return NULL;
995
996 http_msg_invalid:
997 /* invalid message */
998 if (ret_state)
999 *ret_state = HTTP_MSG_ERROR;
1000 return NULL;
1001}
1002
1003
1004/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001005 * This function parses a request line between <ptr> and <end>, starting with
1006 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1007 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1008 * will give undefined results.
1009 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1010 * and that msg->sol points to the beginning of the request.
1011 * If a complete line is found (which implies that at least one CR or LF is
1012 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1013 * returned indicating an incomplete line (which does not mean that parts have
1014 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1015 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1016 * upon next call.
1017 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001018 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001019 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1020 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001021 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001022 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001023const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +01001024 const char *ptr, const char *end,
1025 char **ret_ptr, int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001026{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001027 __label__
1028 http_msg_rqmeth,
1029 http_msg_rqmeth_sp,
1030 http_msg_rquri,
1031 http_msg_rquri_sp,
1032 http_msg_rqver,
1033 http_msg_rqline_eol,
1034 http_msg_ood, /* out of data */
1035 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001036
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001037 switch (state) {
1038 http_msg_rqmeth:
1039 case HTTP_MSG_RQMETH:
1040 if (likely(HTTP_IS_TOKEN(*ptr)))
1041 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001042
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001043 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001044 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001045 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1046 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001047
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001048 if (likely(HTTP_IS_CRLF(*ptr))) {
1049 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001050 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001051 http_msg_req09_uri:
1052 msg->sl.rq.u = ptr - msg_buf;
1053 http_msg_req09_uri_e:
1054 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1055 http_msg_req09_ver:
1056 msg->sl.rq.v = ptr - msg_buf;
1057 msg->sl.rq.v_l = 0;
1058 goto http_msg_rqline_eol;
1059 }
1060 goto http_msg_invalid;
1061
1062 http_msg_rqmeth_sp:
1063 case HTTP_MSG_RQMETH_SP:
1064 if (likely(!HTTP_IS_LWS(*ptr))) {
1065 msg->sl.rq.u = ptr - msg_buf;
1066 goto http_msg_rquri;
1067 }
1068 if (likely(HTTP_IS_SPHT(*ptr)))
1069 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1070 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1071 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 http_msg_rquri:
1074 case HTTP_MSG_RQURI:
1075 if (likely(!HTTP_IS_LWS(*ptr)))
1076 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001078 if (likely(HTTP_IS_SPHT(*ptr))) {
1079 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1080 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1081 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1084 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001085
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001086 http_msg_rquri_sp:
1087 case HTTP_MSG_RQURI_SP:
1088 if (likely(!HTTP_IS_LWS(*ptr))) {
1089 msg->sl.rq.v = ptr - msg_buf;
1090 goto http_msg_rqver;
1091 }
1092 if (likely(HTTP_IS_SPHT(*ptr)))
1093 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1094 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1095 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001096
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001097 http_msg_rqver:
1098 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001099 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001100 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001101
1102 if (likely(HTTP_IS_CRLF(*ptr))) {
1103 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1104 http_msg_rqline_eol:
1105 /* We have seen the end of line. Note that we do not
1106 * necessarily have the \n yet, but at least we know that we
1107 * have EITHER \r OR \n, otherwise the request would not be
1108 * complete. We can then record the request length and return
1109 * to the caller which will be able to register it.
1110 */
1111 msg->sl.rq.l = ptr - msg->sol;
1112 return ptr;
1113 }
1114
1115 /* neither an HTTP_VER token nor a CRLF */
1116 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001117
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001118#ifdef DEBUG_FULL
1119 default:
1120 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1121 exit(1);
1122#endif
1123 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001124
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001125 http_msg_ood:
1126 /* out of data */
1127 if (ret_state)
1128 *ret_state = state;
1129 if (ret_ptr)
1130 *ret_ptr = (char *)ptr;
1131 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001132
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001133 http_msg_invalid:
1134 /* invalid message */
1135 if (ret_state)
1136 *ret_state = HTTP_MSG_ERROR;
1137 return NULL;
1138}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001139
1140
Willy Tarreau8973c702007-01-21 23:58:29 +01001141/*
1142 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001143 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001144 * when data are missing and recalled at the exact same location with no
1145 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001146 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1147 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001148 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001149void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1150{
1151 __label__
1152 http_msg_rqbefore,
1153 http_msg_rqbefore_cr,
1154 http_msg_rqmeth,
1155 http_msg_rqline_end,
1156 http_msg_hdr_first,
1157 http_msg_hdr_name,
1158 http_msg_hdr_l1_sp,
1159 http_msg_hdr_l1_lf,
1160 http_msg_hdr_l1_lws,
1161 http_msg_hdr_val,
1162 http_msg_hdr_l2_lf,
1163 http_msg_hdr_l2_lws,
1164 http_msg_complete_header,
1165 http_msg_last_lf,
1166 http_msg_ood, /* out of data */
1167 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001168
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001169 int state; /* updated only when leaving the FSM */
1170 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001171
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001172 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001173 ptr = buf->lr;
1174 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001175
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001176 if (unlikely(ptr >= end))
1177 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001178
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001179 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001180 /*
1181 * First, states that are specific to the response only.
1182 * We check them first so that request and headers are
1183 * closer to each other (accessed more often).
1184 */
1185 http_msg_rpbefore:
1186 case HTTP_MSG_RPBEFORE:
1187 if (likely(HTTP_IS_TOKEN(*ptr))) {
1188 if (likely(ptr == buf->data)) {
1189 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001190 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001191 } else {
1192#if PARSE_PRESERVE_EMPTY_LINES
1193 /* only skip empty leading lines, don't remove them */
1194 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001195 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001196#else
1197 /* Remove empty leading lines, as recommended by
1198 * RFC2616. This takes a lot of time because we
1199 * must move all the buffer backwards, but this
1200 * is rarely needed. The method above will be
1201 * cleaner when we'll be able to start sending
1202 * the request from any place in the buffer.
1203 */
1204 buf->lr = ptr;
1205 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001206 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001207 msg->sol = buf->data;
1208 ptr = buf->data;
1209 end = buf->r;
1210#endif
1211 }
1212 hdr_idx_init(idx);
1213 state = HTTP_MSG_RPVER;
1214 goto http_msg_rpver;
1215 }
1216
1217 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1218 goto http_msg_invalid;
1219
1220 if (unlikely(*ptr == '\n'))
1221 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1222 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1223 /* stop here */
1224
1225 http_msg_rpbefore_cr:
1226 case HTTP_MSG_RPBEFORE_CR:
1227 EXPECT_LF_HERE(ptr, http_msg_invalid);
1228 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1229 /* stop here */
1230
1231 http_msg_rpver:
1232 case HTTP_MSG_RPVER:
1233 case HTTP_MSG_RPVER_SP:
1234 case HTTP_MSG_RPCODE:
1235 case HTTP_MSG_RPCODE_SP:
1236 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001237 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001238 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001239 if (unlikely(!ptr))
1240 return;
1241
1242 /* we have a full response and we know that we have either a CR
1243 * or an LF at <ptr>.
1244 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001245 //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 +01001246 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1247
1248 msg->sol = ptr;
1249 if (likely(*ptr == '\r'))
1250 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1251 goto http_msg_rpline_end;
1252
1253 http_msg_rpline_end:
1254 case HTTP_MSG_RPLINE_END:
1255 /* msg->sol must point to the first of CR or LF. */
1256 EXPECT_LF_HERE(ptr, http_msg_invalid);
1257 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1258 /* stop here */
1259
1260 /*
1261 * Second, states that are specific to the request only
1262 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001263 http_msg_rqbefore:
1264 case HTTP_MSG_RQBEFORE:
1265 if (likely(HTTP_IS_TOKEN(*ptr))) {
1266 if (likely(ptr == buf->data)) {
1267 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001268 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001269 } else {
1270#if PARSE_PRESERVE_EMPTY_LINES
1271 /* only skip empty leading lines, don't remove them */
1272 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001273 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001274#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 /* Remove empty leading lines, as recommended by
1276 * RFC2616. This takes a lot of time because we
1277 * must move all the buffer backwards, but this
1278 * is rarely needed. The method above will be
1279 * cleaner when we'll be able to start sending
1280 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001281 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001282 buf->lr = ptr;
1283 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001284 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001285 msg->sol = buf->data;
1286 ptr = buf->data;
1287 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001288#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001290 /* we will need this when keep-alive will be supported
1291 hdr_idx_init(idx);
1292 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001293 state = HTTP_MSG_RQMETH;
1294 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001295 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001296
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001297 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1298 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001299
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001300 if (unlikely(*ptr == '\n'))
1301 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1302 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001303 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001304
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001305 http_msg_rqbefore_cr:
1306 case HTTP_MSG_RQBEFORE_CR:
1307 EXPECT_LF_HERE(ptr, http_msg_invalid);
1308 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 http_msg_rqmeth:
1312 case HTTP_MSG_RQMETH:
1313 case HTTP_MSG_RQMETH_SP:
1314 case HTTP_MSG_RQURI:
1315 case HTTP_MSG_RQURI_SP:
1316 case HTTP_MSG_RQVER:
1317 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001318 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 if (unlikely(!ptr))
1320 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001321
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001322 /* we have a full request and we know that we have either a CR
1323 * or an LF at <ptr>.
1324 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001325 //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 +01001326 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001327
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 msg->sol = ptr;
1329 if (likely(*ptr == '\r'))
1330 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001332
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001333 http_msg_rqline_end:
1334 case HTTP_MSG_RQLINE_END:
1335 /* check for HTTP/0.9 request : no version information available.
1336 * msg->sol must point to the first of CR or LF.
1337 */
1338 if (unlikely(msg->sl.rq.v_l == 0))
1339 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001340
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001341 EXPECT_LF_HERE(ptr, http_msg_invalid);
1342 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001343 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001344
Willy Tarreau8973c702007-01-21 23:58:29 +01001345 /*
1346 * Common states below
1347 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001348 http_msg_hdr_first:
1349 case HTTP_MSG_HDR_FIRST:
1350 msg->sol = ptr;
1351 if (likely(!HTTP_IS_CRLF(*ptr))) {
1352 goto http_msg_hdr_name;
1353 }
1354
1355 if (likely(*ptr == '\r'))
1356 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1357 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001358
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001359 http_msg_hdr_name:
1360 case HTTP_MSG_HDR_NAME:
1361 /* assumes msg->sol points to the first char */
1362 if (likely(HTTP_IS_TOKEN(*ptr)))
1363 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001364
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365 if (likely(*ptr == ':')) {
1366 msg->col = ptr - buf->data;
1367 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1368 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001369
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001371
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 http_msg_hdr_l1_sp:
1373 case HTTP_MSG_HDR_L1_SP:
1374 /* assumes msg->sol points to the first char and msg->col to the colon */
1375 if (likely(HTTP_IS_SPHT(*ptr)))
1376 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001377
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001378 /* header value can be basically anything except CR/LF */
1379 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 if (likely(!HTTP_IS_CRLF(*ptr))) {
1382 goto http_msg_hdr_val;
1383 }
1384
1385 if (likely(*ptr == '\r'))
1386 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1387 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001388
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 http_msg_hdr_l1_lf:
1390 case HTTP_MSG_HDR_L1_LF:
1391 EXPECT_LF_HERE(ptr, http_msg_invalid);
1392 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001393
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 http_msg_hdr_l1_lws:
1395 case HTTP_MSG_HDR_L1_LWS:
1396 if (likely(HTTP_IS_SPHT(*ptr))) {
1397 /* replace HT,CR,LF with spaces */
1398 for (; buf->data+msg->sov < ptr; msg->sov++)
1399 buf->data[msg->sov] = ' ';
1400 goto http_msg_hdr_l1_sp;
1401 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001402 /* we had a header consisting only in spaces ! */
1403 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 goto http_msg_complete_header;
1405
1406 http_msg_hdr_val:
1407 case HTTP_MSG_HDR_VAL:
1408 /* assumes msg->sol points to the first char, msg->col to the
1409 * colon, and msg->sov points to the first character of the
1410 * value.
1411 */
1412 if (likely(!HTTP_IS_CRLF(*ptr)))
1413 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001414
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 msg->eol = ptr;
1416 /* Note: we could also copy eol into ->eoh so that we have the
1417 * real header end in case it ends with lots of LWS, but is this
1418 * really needed ?
1419 */
1420 if (likely(*ptr == '\r'))
1421 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1422 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 http_msg_hdr_l2_lf:
1425 case HTTP_MSG_HDR_L2_LF:
1426 EXPECT_LF_HERE(ptr, http_msg_invalid);
1427 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 http_msg_hdr_l2_lws:
1430 case HTTP_MSG_HDR_L2_LWS:
1431 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1432 /* LWS: replace HT,CR,LF with spaces */
1433 for (; msg->eol < ptr; msg->eol++)
1434 *msg->eol = ' ';
1435 goto http_msg_hdr_val;
1436 }
1437 http_msg_complete_header:
1438 /*
1439 * It was a new header, so the last one is finished.
1440 * Assumes msg->sol points to the first char, msg->col to the
1441 * colon, msg->sov points to the first character of the value
1442 * and msg->eol to the first CR or LF so we know how the line
1443 * ends. We insert last header into the index.
1444 */
1445 /*
1446 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1447 write(2, msg->sol, msg->eol-msg->sol);
1448 fprintf(stderr,"\n");
1449 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1452 idx, idx->tail) < 0))
1453 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 msg->sol = ptr;
1456 if (likely(!HTTP_IS_CRLF(*ptr))) {
1457 goto http_msg_hdr_name;
1458 }
1459
1460 if (likely(*ptr == '\r'))
1461 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1462 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 http_msg_last_lf:
1465 case HTTP_MSG_LAST_LF:
1466 /* Assumes msg->sol points to the first of either CR or LF */
1467 EXPECT_LF_HERE(ptr, http_msg_invalid);
1468 ptr++;
1469 buf->lr = ptr;
1470 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001471 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 return;
1473#ifdef DEBUG_FULL
1474 default:
1475 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1476 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001477#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 }
1479 http_msg_ood:
1480 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001481 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001482 buf->lr = ptr;
1483 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001484
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 http_msg_invalid:
1486 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001487 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 return;
1489}
1490
1491/*
1492 * manages the client FSM and its socket. BTW, it also tries to handle the
1493 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1494 * 0 else.
1495 */
1496int process_cli(struct session *t)
1497{
1498 int s = t->srv_state;
1499 int c = t->cli_state;
1500 struct buffer *req = t->req;
1501 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001502
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
1504 cli_stnames[c], srv_stnames[s],
Willy Tarreauf161a342007-04-08 16:59:42 +02001505 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 req->rex.tv_sec, req->rex.tv_usec,
1507 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 if (c == CL_STHEADERS) {
1510 /*
1511 * Now parse the partial (or complete) lines.
1512 * We will check the request syntax, and also join multi-line
1513 * headers. An index of all the lines will be elaborated while
1514 * parsing.
1515 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001516 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001519 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 * req->data + req->eoh = end of processed headers / start of current one
1521 * req->data + req->eol = end of current header or line (LF or CRLF)
1522 * req->lr = first non-visited byte
1523 * req->r = end of data
1524 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001527 struct http_txn *txn = &t->txn;
1528 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001530
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001532 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001533
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 /* 1: we might have to print this header in debug mode */
1535 if (unlikely((global.mode & MODE_DEBUG) &&
1536 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001537 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001539
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001540 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 eol = sol + msg->sl.rq.l;
1542 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001543
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001544 sol += hdr_idx_first_pos(&txn->hdr_idx);
1545 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001546
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001548 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001550 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1551 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 }
1553 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001554
Willy Tarreau58f10d72006-12-04 02:26:12 +01001555
1556 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001558 * If not so, we check the FD and buffer states before leaving.
1559 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1561 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001562 *
1563 */
1564
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001565 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 /*
1567 * First, let's catch bad requests.
1568 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001569 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001571
1572 /* 1: Since we are in header mode, if there's no space
1573 * left for headers, we won't be able to free more
1574 * later, so the session will never terminate. We
1575 * must terminate it now.
1576 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 if (unlikely(req->l >= req->rlim - req->data)) {
1578 /* FIXME: check if URI is set and return Status
1579 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001580 */
Willy Tarreau06619262006-12-17 08:37:22 +01001581 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001582 }
1583
1584 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1586 /* read error, or last read : give up. */
Willy Tarreaufa645582007-06-03 15:59:52 +02001587 buffer_shutr(req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001588 fd_delete(t->cli_fd);
1589 t->cli_state = CL_STCLOSE;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001590 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001591 if (!(t->flags & SN_ERR_MASK))
1592 t->flags |= SN_ERR_CLICL;
1593 if (!(t->flags & SN_FINST_MASK))
1594 t->flags |= SN_FINST_R;
1595 return 1;
1596 }
1597
1598 /* 3: has the read timeout expired ? */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001599 else if (unlikely(tv_isle(&req->rex, &now))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001600 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001601 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001602 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001603 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001604 if (!(t->flags & SN_ERR_MASK))
1605 t->flags |= SN_ERR_CLITO;
1606 if (!(t->flags & SN_FINST_MASK))
1607 t->flags |= SN_FINST_R;
1608 return 1;
1609 }
1610
1611 /* 4: do we need to re-enable the read socket ? */
Willy Tarreau66319382007-04-08 17:17:37 +02001612 else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001613 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreau58f10d72006-12-04 02:26:12 +01001614 * full. We cannot loop here since stream_sock_read will disable it only if
1615 * req->l == rlim-data
1616 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001617 if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout))
Willy Tarreau58f10d72006-12-04 02:26:12 +01001618 tv_eternity(&req->rex);
1619 }
1620 return t->cli_state != CL_STHEADERS;
1621 }
1622
1623
1624 /****************************************************************
1625 * More interesting part now : we know that we have a complete *
1626 * request which at least looks like HTTP. We have an indicator *
1627 * of each header's length, so we can parse them quickly. *
1628 ****************************************************************/
1629
Willy Tarreau9cdde232007-05-02 20:58:19 +02001630 /* ensure we keep this pointer to the beginning of the message */
1631 msg->sol = req->data + msg->som;
1632
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001633 /*
1634 * 1: identify the method
1635 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001636 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001637
1638 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001640 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001642 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 if (unlikely((t->fe->monitor_uri_len != 0) &&
1644 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1645 !memcmp(&req->data[msg->sl.rq.u],
1646 t->fe->monitor_uri,
1647 t->fe->monitor_uri_len))) {
1648 /*
1649 * We have found the monitor URI
1650 */
1651 t->flags |= SN_MONITOR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001652 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653 client_retnclose(t, &http_200_chunk);
1654 goto return_prx_cond;
1655 }
1656
1657 /*
1658 * 3: Maybe we have to copy the original REQURI for the logs ?
1659 * Note: we cannot log anymore if the request has been
1660 * classified as invalid.
1661 */
1662 if (unlikely(t->logs.logwait & LW_REQ)) {
1663 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001664 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001665 int urilen = msg->sl.rq.l;
1666
1667 if (urilen >= REQURI_LEN)
1668 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001669 memcpy(txn->uri, &req->data[msg->som], urilen);
1670 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671
1672 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001673 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 } else {
1675 Alert("HTTP logging : out of memory.\n");
1676 }
1677 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001678
Willy Tarreau06619262006-12-17 08:37:22 +01001679
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001680 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1681 if (unlikely(msg->sl.rq.v_l == 0)) {
1682 int delta;
1683 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001684 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 cur_end = msg->sol + msg->sl.rq.l;
1686 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001687
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001688 if (msg->sl.rq.u_l == 0) {
1689 /* if no URI was set, add "/" */
1690 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1691 cur_end += delta;
1692 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001693 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 /* add HTTP version */
1695 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1696 msg->eoh += delta;
1697 cur_end += delta;
1698 cur_end = (char *)http_parse_reqline(msg, req->data,
1699 HTTP_MSG_RQMETH,
1700 msg->sol, cur_end + 1,
1701 NULL, NULL);
1702 if (unlikely(!cur_end))
1703 goto return_bad_req;
1704
1705 /* we have a full HTTP/1.0 request now and we know that
1706 * we have either a CR or an LF at <ptr>.
1707 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001708 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001709 }
1710
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001711
1712 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001713 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001714 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001715 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001716
1717 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001718 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001719 * As opposed to version 1.2, now they will be evaluated in the
1720 * filters order and not in the header order. This means that
1721 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001722 *
1723 * We can now check whether we want to switch to another
1724 * backend, in which case we will re-check the backend's
1725 * filters and various options. In order to support 3-level
1726 * switching, here's how we should proceed :
1727 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001728 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001729 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001730 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001731 * There cannot be any switch from there, so ->be cannot be
1732 * changed anymore.
1733 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001734 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001735 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001736 * The response path will be able to apply either ->be, or
1737 * ->be then ->fe filters in order to match the reverse of
1738 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001739 */
1740
Willy Tarreau06619262006-12-17 08:37:22 +01001741 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001742 struct acl_cond *cond;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001743 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001744 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001745
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001746 /* first check whether we have some ACLs set to block this request */
1747 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001748 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001749 if (cond->pol == ACL_COND_UNLESS)
1750 ret = !ret;
1751
1752 if (ret) {
1753 txn->status = 403;
1754 /* let's log the request time */
1755 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
1756 client_retnclose(t, error_message(t, HTTP_ERR_403));
1757 goto return_prx_cond;
1758 }
1759 }
1760
Willy Tarreau06619262006-12-17 08:37:22 +01001761 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01001762 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001763 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
1764 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001765 }
1766
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001767 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
1768 /* to ensure correct connection accounting on
1769 * the backend, we count the connection for the
1770 * one managing the queue.
1771 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001772 t->be->beconn++;
1773 if (t->be->beconn > t->be->beconn_max)
1774 t->be->beconn_max = t->be->beconn;
1775 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001776 t->flags |= SN_BE_ASSIGNED;
1777 }
1778
Willy Tarreau06619262006-12-17 08:37:22 +01001779 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01001780 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01001781 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001782 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001783 /* let's log the request time */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001784 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreau80587432006-12-24 17:47:20 +01001785 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001786 goto return_prx_cond;
1787 }
1788
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001789 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001790 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001791 !(t->flags & SN_CONN_CLOSED)) {
1792 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001793 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001794 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01001795
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001796 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 old_idx = 0;
1798
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001799 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1800 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001801 cur_ptr = cur_next;
1802 cur_end = cur_ptr + cur_hdr->len;
1803 cur_next = cur_end + cur_hdr->cr + 1;
1804
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001805 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1806 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001807 /* 3 possibilities :
1808 * - we have already set Connection: close,
1809 * so we remove this line.
1810 * - we have not yet set Connection: close,
1811 * but this line indicates close. We leave
1812 * it untouched and set the flag.
1813 * - we have not yet set Connection: close,
1814 * and this line indicates non-close. We
1815 * replace it.
1816 */
1817 if (t->flags & SN_CONN_CLOSED) {
1818 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001819 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001821 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1822 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001823 cur_hdr->len = 0;
1824 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001825 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1826 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1827 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 cur_next += delta;
1829 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001830 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001831 }
1832 t->flags |= SN_CONN_CLOSED;
1833 }
1834 }
1835 old_idx = cur_idx;
1836 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02001837 }
1838 /* add request headers from the rule sets in the same order */
1839 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
1840 if (unlikely(http_header_add_tail(req,
1841 &txn->req,
1842 &txn->hdr_idx,
1843 rule_set->req_add[cur_idx])) < 0)
1844 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01001845 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001846
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001847 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01001848 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001849 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01001850 /* we have to check the URI and auth for this request */
1851 if (stats_check_uri_auth(t, rule_set))
1852 return 1;
1853 }
1854
Willy Tarreau55ea7572007-06-17 19:56:27 +02001855 /* now check whether we have some switching rules for this request */
1856 if (!(t->flags & SN_BE_ASSIGNED)) {
1857 struct switching_rule *rule;
1858
1859 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
1860 int ret;
1861
1862 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
1863 if (cond->pol == ACL_COND_UNLESS)
1864 ret = !ret;
1865
1866 if (ret) {
1867 t->be = rule->be.backend;
1868 t->be->beconn++;
1869 if (t->be->beconn > t->be->beconn_max)
1870 t->be->beconn_max = t->be->beconn;
1871 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001872
1873 /* assign new parameters to the session from the new backend */
1874 t->rep->rto = t->req->wto = t->be->srvtimeout;
1875 t->req->cto = t->be->contimeout;
1876 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02001877 t->flags |= SN_BE_ASSIGNED;
1878 break;
1879 }
1880 }
1881 }
1882
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001883 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
1884 /* No backend was set, but there was a default
1885 * backend set in the frontend, so we use it and
1886 * loop again.
1887 */
1888 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001889 t->be->beconn++;
1890 if (t->be->beconn > t->be->beconn_max)
1891 t->be->beconn_max = t->be->beconn;
1892 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001893
1894 /* assign new parameters to the session from the new backend */
1895 t->rep->rto = t->req->wto = t->be->srvtimeout;
1896 t->req->cto = t->be->contimeout;
1897 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001898 t->flags |= SN_BE_ASSIGNED;
1899 }
1900 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01001901
Willy Tarreau58f10d72006-12-04 02:26:12 +01001902
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001903 if (!(t->flags & SN_BE_ASSIGNED)) {
1904 /* To ensure correct connection accounting on
1905 * the backend, we count the connection for the
1906 * one managing the queue.
1907 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001908 t->be->beconn++;
1909 if (t->be->beconn > t->be->beconn_max)
1910 t->be->beconn_max = t->be->beconn;
1911 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001912 t->flags |= SN_BE_ASSIGNED;
1913 }
1914
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001915 /*
1916 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01001917 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001918 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01001919 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +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 Tarreau58f10d72006-12-04 02:26:12 +01001924
Willy Tarreau2a324282006-12-05 00:05:46 +01001925 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001926 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01001927 * so let's do the same now.
1928 */
1929
1930 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001931 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001932 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01001933 }
1934
1935
1936 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001937 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01001938 * Note that doing so might move headers in the request, but
1939 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01001940 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01001941 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001942 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01001943 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001944
Willy Tarreau58f10d72006-12-04 02:26:12 +01001945
Willy Tarreau2a324282006-12-05 00:05:46 +01001946 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001947 * 9: add X-Forwarded-For if either the frontend or the backend
1948 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01001949 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001950 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001951 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001952 /* Add an X-Forwarded-For header unless the source IP is
1953 * in the 'except' network range.
1954 */
1955 if ((!t->fe->except_mask.s_addr ||
1956 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
1957 != t->fe->except_net.s_addr) &&
1958 (!t->be->except_mask.s_addr ||
1959 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
1960 != t->be->except_net.s_addr)) {
1961 int len;
1962 unsigned char *pn;
1963 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01001964
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001965 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d",
1966 pn[0], pn[1], pn[2], pn[3]);
1967
1968 if (unlikely(http_header_add_tail2(req, &txn->req,
1969 &txn->hdr_idx, trash, len)) < 0)
1970 goto return_bad_req;
1971 }
Willy Tarreau2a324282006-12-05 00:05:46 +01001972 }
1973 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001974 /* FIXME: for the sake of completeness, we should also support
1975 * 'except' here, although it is mostly useless in this case.
1976 */
Willy Tarreau2a324282006-12-05 00:05:46 +01001977 int len;
1978 char pn[INET6_ADDRSTRLEN];
1979 inet_ntop(AF_INET6,
1980 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
1981 pn, sizeof(pn));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001982 len = sprintf(trash, "X-Forwarded-For: %s", pn);
1983 if (unlikely(http_header_add_tail2(req, &txn->req,
1984 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001985 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01001986 }
1987 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001988
Willy Tarreau2a324282006-12-05 00:05:46 +01001989 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001990 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02001991 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01001992 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02001993 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001994 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02001995 if ((unlikely(msg->sl.rq.v_l != 8) ||
1996 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
1997 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001998 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001999 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002000 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002001 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002002
Willy Tarreau2a324282006-12-05 00:05:46 +01002003 /*************************************************************
2004 * OK, that's finished for the headers. We have done what we *
2005 * could. Let's switch to the DATA state. *
2006 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002007
Willy Tarreau2a324282006-12-05 00:05:46 +01002008 t->cli_state = CL_STDATA;
2009 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002010
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002011 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002012
Willy Tarreaud825eef2007-05-12 22:35:00 +02002013 if (!tv_isset(&t->fe->clitimeout) ||
2014 (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002015 /* If the client has no timeout, or if the server is not ready yet,
2016 * and we know for sure that it can expire, then it's cleaner to
2017 * disable the timeout on the client side so that too low values
2018 * cannot make the sessions abort too early.
2019 *
2020 * FIXME-20050705: the server needs a way to re-enable this time-out
2021 * when it switches its state, otherwise a client can stay connected
2022 * indefinitely. This now seems to be OK.
2023 */
2024 tv_eternity(&req->rex);
2025 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002026
Willy Tarreau2a324282006-12-05 00:05:46 +01002027 /* When a connection is tarpitted, we use the queue timeout for the
2028 * tarpit delay, which currently happens to be the server's connect
2029 * timeout. If unset, then set it to zero because we really want it
2030 * to expire at one moment.
2031 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002032 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002033 t->req->l = 0;
2034 /* flush the request so that we can drop the connection early
2035 * if the client closes first.
2036 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002037 if (!tv_add_ifset(&req->cex, &now, &t->be->contimeout))
Willy Tarreaud825eef2007-05-12 22:35:00 +02002038 req->cex = now;
Willy Tarreau2a324282006-12-05 00:05:46 +01002039 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002040
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002041 /* OK let's go on with the BODY now */
Willy Tarreau06619262006-12-17 08:37:22 +01002042 goto process_data;
2043
2044 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002045 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002046 txn->status = 400;
Willy Tarreau80587432006-12-24 17:47:20 +01002047 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002048 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002049 return_prx_cond:
2050 if (!(t->flags & SN_ERR_MASK))
2051 t->flags |= SN_ERR_PRXCOND;
2052 if (!(t->flags & SN_FINST_MASK))
2053 t->flags |= SN_FINST_R;
2054 return 1;
2055
Willy Tarreaubaaee002006-06-26 02:48:02 +02002056 }
2057 else if (c == CL_STDATA) {
2058 process_data:
2059 /* FIXME: this error handling is partly buggy because we always report
2060 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
2061 * or HEADER phase. BTW, it's not logical to expire the client while
2062 * we're waiting for the server to connect.
2063 */
2064 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002065 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002066 buffer_shutr(req);
2067 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002068 fd_delete(t->cli_fd);
2069 t->cli_state = CL_STCLOSE;
2070 if (!(t->flags & SN_ERR_MASK))
2071 t->flags |= SN_ERR_CLICL;
2072 if (!(t->flags & SN_FINST_MASK)) {
2073 if (t->pend_pos)
2074 t->flags |= SN_FINST_Q;
2075 else if (s == SV_STCONN)
2076 t->flags |= SN_FINST_C;
2077 else
2078 t->flags |= SN_FINST_D;
2079 }
2080 return 1;
2081 }
2082 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002083 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002084 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002085 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002086 t->cli_state = CL_STSHUTR;
2087 return 1;
2088 }
2089 /* last server read and buffer empty */
2090 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002091 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002092 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002093 shutdown(t->cli_fd, SHUT_WR);
2094 /* We must ensure that the read part is still alive when switching
2095 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002096 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002097 tv_add_ifset(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002098 t->cli_state = CL_STSHUTW;
2099 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2100 return 1;
2101 }
2102 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002103 else if (tv_isle(&req->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002104 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002105 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002106 t->cli_state = CL_STSHUTR;
2107 if (!(t->flags & SN_ERR_MASK))
2108 t->flags |= SN_ERR_CLITO;
2109 if (!(t->flags & SN_FINST_MASK)) {
2110 if (t->pend_pos)
2111 t->flags |= SN_FINST_Q;
2112 else if (s == SV_STCONN)
2113 t->flags |= SN_FINST_C;
2114 else
2115 t->flags |= SN_FINST_D;
2116 }
2117 return 1;
2118 }
2119 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002120 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002121 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002122 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002123 shutdown(t->cli_fd, SHUT_WR);
2124 /* We must ensure that the read part is still alive when switching
2125 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002126 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002127 tv_add_ifset(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002128
2129 t->cli_state = CL_STSHUTW;
2130 if (!(t->flags & SN_ERR_MASK))
2131 t->flags |= SN_ERR_CLITO;
2132 if (!(t->flags & SN_FINST_MASK)) {
2133 if (t->pend_pos)
2134 t->flags |= SN_FINST_Q;
2135 else if (s == SV_STCONN)
2136 t->flags |= SN_FINST_C;
2137 else
2138 t->flags |= SN_FINST_D;
2139 }
2140 return 1;
2141 }
2142
2143 if (req->l >= req->rlim - req->data) {
2144 /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02002145 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002146 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002147 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002148 }
2149 } else {
2150 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002151 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaud825eef2007-05-12 22:35:00 +02002152 if (!tv_isset(&t->fe->clitimeout) ||
2153 (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout)))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002154 /* If the client has no timeout, or if the server not ready yet, and we
2155 * know for sure that it can expire, then it's cleaner to disable the
2156 * timeout on the client side so that too low values cannot make the
2157 * sessions abort too early.
2158 */
Willy Tarreaud7971282006-07-29 18:36:34 +02002159 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002160 else
Willy Tarreaud825eef2007-05-12 22:35:00 +02002161 tv_add(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002162 }
2163 }
2164
2165 if ((rep->l == 0) ||
2166 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002167 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2168 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002169 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002170 }
2171 } else {
2172 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002173 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2174 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002175 if (tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002176 /* FIXME: to prevent the client from expiring read timeouts during writes,
2177 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002178 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002179 }
2180 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002181 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002182 }
2183 }
2184 return 0; /* other cases change nothing */
2185 }
2186 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002187 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002188 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002189 fd_delete(t->cli_fd);
2190 t->cli_state = CL_STCLOSE;
2191 if (!(t->flags & SN_ERR_MASK))
2192 t->flags |= SN_ERR_CLICL;
2193 if (!(t->flags & SN_FINST_MASK)) {
2194 if (t->pend_pos)
2195 t->flags |= SN_FINST_Q;
2196 else if (s == SV_STCONN)
2197 t->flags |= SN_FINST_C;
2198 else
2199 t->flags |= SN_FINST_D;
2200 }
2201 return 1;
2202 }
2203 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
2204 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002205 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002206 fd_delete(t->cli_fd);
2207 t->cli_state = CL_STCLOSE;
2208 return 1;
2209 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002210 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002211 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002212 fd_delete(t->cli_fd);
2213 t->cli_state = CL_STCLOSE;
2214 if (!(t->flags & SN_ERR_MASK))
2215 t->flags |= SN_ERR_CLITO;
2216 if (!(t->flags & SN_FINST_MASK)) {
2217 if (t->pend_pos)
2218 t->flags |= SN_FINST_Q;
2219 else if (s == SV_STCONN)
2220 t->flags |= SN_FINST_C;
2221 else
2222 t->flags |= SN_FINST_D;
2223 }
2224 return 1;
2225 }
2226
2227 if (t->flags & SN_SELF_GEN) {
2228 produce_content(t);
2229 if (rep->l == 0) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002230 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002231 fd_delete(t->cli_fd);
2232 t->cli_state = CL_STCLOSE;
2233 return 1;
2234 }
2235 }
2236
2237 if ((rep->l == 0)
2238 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002239 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2240 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002241 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002242 }
2243 } else {
2244 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002245 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2246 /* restart writing */
Willy Tarreau33014d02007-06-03 15:25:37 +02002247 if (!tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02002248 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002249 }
2250 }
2251 return 0;
2252 }
2253 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002254 if (req->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002255 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002256 fd_delete(t->cli_fd);
2257 t->cli_state = CL_STCLOSE;
2258 if (!(t->flags & SN_ERR_MASK))
2259 t->flags |= SN_ERR_CLICL;
2260 if (!(t->flags & SN_FINST_MASK)) {
2261 if (t->pend_pos)
2262 t->flags |= SN_FINST_Q;
2263 else if (s == SV_STCONN)
2264 t->flags |= SN_FINST_C;
2265 else
2266 t->flags |= SN_FINST_D;
2267 }
2268 return 1;
2269 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002270 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002271 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002272 fd_delete(t->cli_fd);
2273 t->cli_state = CL_STCLOSE;
2274 return 1;
2275 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002276 else if (tv_isle(&req->rex, &now)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002277 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002278 fd_delete(t->cli_fd);
2279 t->cli_state = CL_STCLOSE;
2280 if (!(t->flags & SN_ERR_MASK))
2281 t->flags |= SN_ERR_CLITO;
2282 if (!(t->flags & SN_FINST_MASK)) {
2283 if (t->pend_pos)
2284 t->flags |= SN_FINST_Q;
2285 else if (s == SV_STCONN)
2286 t->flags |= SN_FINST_C;
2287 else
2288 t->flags |= SN_FINST_D;
2289 }
2290 return 1;
2291 }
2292 else if (req->l >= req->rlim - req->data) {
2293 /* no room to read more data */
2294
2295 /* FIXME-20050705: is it possible for a client to maintain a session
2296 * after the timeout by sending more data after it receives a close ?
2297 */
2298
Willy Tarreau66319382007-04-08 17:17:37 +02002299 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002300 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002301 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002302 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2303 }
2304 } else {
2305 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002306 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002307 if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02002308 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002309 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2310 }
2311 }
2312 return 0;
2313 }
2314 else { /* CL_STCLOSE: nothing to do */
2315 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2316 int len;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002317 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 +02002318 write(1, trash, len);
2319 }
2320 return 0;
2321 }
2322 return 0;
2323}
2324
2325
2326/*
2327 * manages the server FSM and its socket. It returns 1 if a state has changed
2328 * (and a resync may be needed), 0 else.
2329 */
2330int process_srv(struct session *t)
2331{
2332 int s = t->srv_state;
2333 int c = t->cli_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002334 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002335 struct buffer *req = t->req;
2336 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002337 int conn_err;
2338
2339#ifdef DEBUG_FULL
2340 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
2341#endif
Willy Tarreauee991362007-05-14 14:37:50 +02002342
2343#if 0
2344 fprintf(stderr,"%s:%d fe->clito=%d.%d, fe->conto=%d.%d, fe->srvto=%d.%d\n",
2345 __FUNCTION__, __LINE__,
2346 t->fe->clitimeout.tv_sec, t->fe->clitimeout.tv_usec,
2347 t->fe->contimeout.tv_sec, t->fe->contimeout.tv_usec,
2348 t->fe->srvtimeout.tv_sec, t->fe->srvtimeout.tv_usec);
2349 fprintf(stderr,"%s:%d be->clito=%d.%d, be->conto=%d.%d, be->srvto=%d.%d\n",
2350 __FUNCTION__, __LINE__,
2351 t->be->clitimeout.tv_sec, t->be->clitimeout.tv_usec,
2352 t->be->contimeout.tv_sec, t->be->contimeout.tv_usec,
2353 t->be->srvtimeout.tv_sec, t->be->srvtimeout.tv_usec);
2354
2355 fprintf(stderr,"%s:%d req->cto=%d.%d, req->rto=%d.%d, req->wto=%d.%d\n",
2356 __FUNCTION__, __LINE__,
2357 req->cto.tv_sec, req->cto.tv_usec,
2358 req->rto.tv_sec, req->rto.tv_usec,
2359 req->wto.tv_sec, req->wto.tv_usec);
2360
2361 fprintf(stderr,"%s:%d rep->cto=%d.%d, rep->rto=%d.%d, rep->wto=%d.%d\n",
2362 __FUNCTION__, __LINE__,
2363 rep->cto.tv_sec, rep->cto.tv_usec,
2364 rep->rto.tv_sec, rep->rto.tv_usec,
2365 rep->wto.tv_sec, rep->wto.tv_usec);
2366#endif
2367
Willy Tarreaubaaee002006-06-26 02:48:02 +02002368 //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 +02002369 //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
2370 //EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002371 //);
2372 if (s == SV_STIDLE) {
2373 if (c == CL_STHEADERS)
2374 return 0; /* stay in idle, waiting for data to reach the client side */
2375 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2376 (c == CL_STSHUTR &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002377 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002378 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002379 if (t->pend_pos)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002380 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002381 /* note that this must not return any error because it would be able to
2382 * overwrite the client_retnclose() output.
2383 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002384 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002385 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002386 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002387 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 +02002388
2389 return 1;
2390 }
2391 else {
Willy Tarreau3d300592007-03-18 18:34:41 +01002392 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002393 /* This connection is being tarpitted. The CLIENT side has
2394 * already set the connect expiration date to the right
2395 * timeout. We just have to check that it has not expired.
2396 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002397 if (!tv_isle(&req->cex, &now))
Willy Tarreaub8750a82006-09-03 09:56:00 +02002398 return 0;
2399
2400 /* We will set the queue timer to the time spent, just for
2401 * logging purposes. We fake a 500 server error, so that the
2402 * attacker will not suspect his connection has been tarpitted.
2403 * It will not cause trouble to the logs because we can exclude
2404 * the tarpitted connections by filtering on the 'PT' status flags.
2405 */
2406 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002407 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub8750a82006-09-03 09:56:00 +02002408 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002409 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002410 return 1;
2411 }
2412
Willy Tarreaubaaee002006-06-26 02:48:02 +02002413 /* Right now, we will need to create a connection to the server.
2414 * We might already have tried, and got a connection pending, in
2415 * which case we will not do anything till it's pending. It's up
2416 * to any other session to release it and wake us up again.
2417 */
2418 if (t->pend_pos) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002419 if (!tv_isle(&req->cex, &now))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002420 return 0;
2421 else {
2422 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002423 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002424 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002425 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002426 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002427 if (t->srv)
2428 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01002429 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002430 return 1;
2431 }
2432 }
2433
2434 do {
2435 /* first, get a connection */
2436 if (srv_redispatch_connect(t))
2437 return t->srv_state != SV_STIDLE;
2438
2439 /* try to (re-)connect to the server, and fail if we expire the
2440 * number of retries.
2441 */
2442 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002443 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002444 return t->srv_state != SV_STIDLE;
2445 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002446 } while (1);
2447 }
2448 }
2449 else if (s == SV_STCONN) { /* connection in progress */
2450 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2451 (c == CL_STSHUTR &&
Willy Tarreauc9b654b2007-05-08 14:46:53 +02002452 ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
2453 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002454 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002455 fd_delete(t->srv_fd);
2456 if (t->srv)
2457 t->srv->cur_sess--;
2458
2459 /* note that this must not return any error because it would be able to
2460 * overwrite the client_retnclose() output.
2461 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002462 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002463 return 1;
2464 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002465 if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002466 //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 +02002467 return 0; /* nothing changed */
2468 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002469 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002470 /* timeout, asynchronous connect error or first write error */
2471 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2472
2473 fd_delete(t->srv_fd);
2474 if (t->srv)
2475 t->srv->cur_sess--;
2476
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002477 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002478 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2479 else
2480 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2481
2482 /* ensure that we have enough retries left */
2483 if (srv_count_retry_down(t, conn_err))
2484 return 1;
2485
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002486 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002487 /* We're on our last chance, and the REDISP option was specified.
2488 * We will ignore cookie and force to balance or use the dispatcher.
2489 */
2490 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002491 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002492 task_wakeup(t->srv->queue_mgt);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002493
2494 if (t->srv)
2495 t->srv->failed_conns++;
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002496 t->be->redispatches++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002497
2498 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
2499 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreaua5e65752007-03-18 20:53:22 +01002500 http_flush_cookie_flags(txn);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002501
2502 /* first, get a connection */
2503 if (srv_redispatch_connect(t))
2504 return t->srv_state != SV_STIDLE;
2505 }
2506
Willy Tarreaubaaee002006-06-26 02:48:02 +02002507 do {
2508 /* Now we will try to either reconnect to the same server or
2509 * connect to another server. If the connection gets queued
2510 * because all servers are saturated, then we will go back to
2511 * the SV_STIDLE state.
2512 */
2513 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002514 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002515 return t->srv_state != SV_STCONN;
2516 }
2517
2518 /* we need to redispatch the connection to another server */
2519 if (srv_redispatch_connect(t))
2520 return t->srv_state != SV_STCONN;
2521 } while (1);
2522 }
2523 else { /* no error or write 0 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002524 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002525
2526 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2527 if (req->l == 0) /* nothing to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002528 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002529 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002530 } else /* need the right to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002531 EV_FD_SET(t->srv_fd, DIR_WR);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002532 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002533 /* FIXME: to prevent the server from expiring read timeouts during writes,
2534 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002535 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002536 }
2537 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002538 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002539 }
2540
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002541 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreauf161a342007-04-08 16:59:42 +02002542 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002543 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02002544 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002545
2546 t->srv_state = SV_STDATA;
2547 if (t->srv)
2548 t->srv->cum_sess++;
2549 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2550
2551 /* if the user wants to log as soon as possible, without counting
2552 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002553 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002554 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
Willy Tarreau42250582007-04-01 01:30:43 +02002555 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002556 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002557#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002558 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002559 /* TCP splicing supported by both FE and BE */
2560 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2561 }
2562#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002563 }
2564 else {
2565 t->srv_state = SV_STHEADERS;
2566 if (t->srv)
2567 t->srv->cum_sess++;
2568 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002569 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2570 /* reset hdr_idx which was already initialized by the request.
2571 * right now, the http parser does it.
2572 * hdr_idx_init(&t->txn.hdr_idx);
2573 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002574 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002575 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002576 return 1;
2577 }
2578 }
2579 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002580 /*
2581 * Now parse the partial (or complete) lines.
2582 * We will check the response syntax, and also join multi-line
2583 * headers. An index of all the lines will be elaborated while
2584 * parsing.
2585 *
2586 * For the parsing, we use a 28 states FSM.
2587 *
2588 * Here is the information we currently have :
2589 * rep->data + req->som = beginning of response
2590 * rep->data + req->eoh = end of processed headers / start of current one
2591 * rep->data + req->eol = end of current header or line (LF or CRLF)
2592 * rep->lr = first non-visited byte
2593 * rep->r = end of data
2594 */
2595
2596 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002597 struct http_msg *msg = &txn->rsp;
2598 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002599
Willy Tarreaua15645d2007-03-18 16:22:39 +01002600 if (likely(rep->lr < rep->r))
2601 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002602
Willy Tarreaua15645d2007-03-18 16:22:39 +01002603 /* 1: we might have to print this header in debug mode */
2604 if (unlikely((global.mode & MODE_DEBUG) &&
2605 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2606 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2607 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002608
Willy Tarreaua15645d2007-03-18 16:22:39 +01002609 sol = rep->data + msg->som;
2610 eol = sol + msg->sl.rq.l;
2611 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002612
Willy Tarreaua15645d2007-03-18 16:22:39 +01002613 sol += hdr_idx_first_pos(&txn->hdr_idx);
2614 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002615
Willy Tarreaua15645d2007-03-18 16:22:39 +01002616 while (cur_idx) {
2617 eol = sol + txn->hdr_idx.v[cur_idx].len;
2618 debug_hdr("srvhdr", t, sol, eol);
2619 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2620 cur_idx = txn->hdr_idx.v[cur_idx].next;
2621 }
2622 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002623
Willy Tarreaubaaee002006-06-26 02:48:02 +02002624
Willy Tarreau66319382007-04-08 17:17:37 +02002625 if ((rep->l < rep->rlim - rep->data) && EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002626 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreaua15645d2007-03-18 16:22:39 +01002627 * full. We cannot loop here since stream_sock_read will disable it only if
2628 * rep->l == rlim-data
2629 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002630 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002631 tv_eternity(&rep->rex);
2632 }
2633
2634
2635 /*
2636 * Now we quickly check if we have found a full valid response.
2637 * If not so, we check the FD and buffer states before leaving.
2638 * A full response is indicated by the fact that we have seen
2639 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2640 * responses are checked first.
2641 *
2642 * Depending on whether the client is still there or not, we
2643 * may send an error response back or not. Note that normally
2644 * we should only check for HTTP status there, and check I/O
2645 * errors somewhere else.
2646 */
2647
2648 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2649
2650 /* Invalid response, or read error or write error */
2651 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2652 (req->flags & BF_WRITE_ERROR) ||
2653 (rep->flags & BF_READ_ERROR))) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002654 buffer_shutr(rep);
2655 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002656 fd_delete(t->srv_fd);
2657 if (t->srv) {
2658 t->srv->cur_sess--;
2659 t->srv->failed_resp++;
2660 }
2661 t->be->failed_resp++;
2662 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002663 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002664 client_return(t, error_message(t, HTTP_ERR_502));
2665 if (!(t->flags & SN_ERR_MASK))
2666 t->flags |= SN_ERR_SRVCL;
2667 if (!(t->flags & SN_FINST_MASK))
2668 t->flags |= SN_FINST_H;
2669 /* We used to have a free connection slot. Since we'll never use it,
2670 * we have to inform the server that it may be used by another session.
2671 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002672 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002673 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002674
Willy Tarreaua15645d2007-03-18 16:22:39 +01002675 return 1;
2676 }
2677
2678 /* end of client write or end of server read.
2679 * since we are in header mode, if there's no space left for headers, we
2680 * won't be able to free more later, so the session will never terminate.
2681 */
2682 else if (unlikely(rep->flags & BF_READ_NULL ||
2683 c == CL_STSHUTW || c == CL_STCLOSE ||
2684 rep->l >= rep->rlim - rep->data)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002685 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002686 buffer_shutr(rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002687 t->srv_state = SV_STSHUTR;
2688 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2689 return 1;
2690 }
2691
2692 /* read timeout : return a 504 to the client.
2693 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002694 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002695 tv_isle(&rep->rex, &now))) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002696 buffer_shutr(rep);
2697 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002698 fd_delete(t->srv_fd);
2699 if (t->srv) {
2700 t->srv->cur_sess--;
2701 t->srv->failed_resp++;
2702 }
2703 t->be->failed_resp++;
2704 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002705 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002706 client_return(t, error_message(t, HTTP_ERR_504));
2707 if (!(t->flags & SN_ERR_MASK))
2708 t->flags |= SN_ERR_SRVTO;
2709 if (!(t->flags & SN_FINST_MASK))
2710 t->flags |= SN_FINST_H;
2711 /* We used to have a free connection slot. Since we'll never use it,
2712 * we have to inform the server that it may be used by another session.
2713 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002714 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002715 task_wakeup(t->srv->queue_mgt);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002716 return 1;
2717 }
2718
2719 /* last client read and buffer empty */
2720 /* FIXME!!! here, we don't want to switch to SHUTW if the
2721 * client shuts read too early, because we may still have
2722 * some work to do on the headers.
2723 * The side-effect is that if the client completely closes its
2724 * connection during SV_STHEADER, the connection to the server
2725 * is kept until a response comes back or the timeout is reached.
2726 */
2727 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2728 (req->l == 0))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002729 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002730 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002731
2732 /* We must ensure that the read part is still
2733 * alive when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002734 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002735 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002736
2737 shutdown(t->srv_fd, SHUT_WR);
2738 t->srv_state = SV_STSHUTW;
2739 return 1;
2740 }
2741
2742 /* write timeout */
2743 /* FIXME!!! here, we don't want to switch to SHUTW if the
2744 * client shuts read too early, because we may still have
2745 * some work to do on the headers.
2746 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002747 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002748 tv_isle(&req->wex, &now))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002749 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002750 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002751 shutdown(t->srv_fd, SHUT_WR);
2752 /* We must ensure that the read part is still alive
2753 * when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002754 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002755 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002756
2757 t->srv_state = SV_STSHUTW;
2758 if (!(t->flags & SN_ERR_MASK))
2759 t->flags |= SN_ERR_SRVTO;
2760 if (!(t->flags & SN_FINST_MASK))
2761 t->flags |= SN_FINST_H;
2762 return 1;
2763 }
2764
2765 /*
2766 * And now the non-error cases.
2767 */
2768
2769 /* Data remaining in the request buffer.
2770 * This happens during the first pass here, and during
2771 * long posts.
2772 */
2773 else if (likely(req->l)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002774 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
2775 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002776 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002777 /* FIXME: to prevent the server from expiring read timeouts during writes,
2778 * we refresh it. */
2779 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002780 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002781 else
2782 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002783 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002784 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002785
Willy Tarreaua15645d2007-03-18 16:22:39 +01002786 /* nothing left in the request buffer */
2787 else {
Willy Tarreau66319382007-04-08 17:17:37 +02002788 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
2789 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002790 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002791 }
2792 }
2793
2794 return t->srv_state != SV_STHEADERS;
2795 }
2796
2797
2798 /*****************************************************************
2799 * More interesting part now : we know that we have a complete *
2800 * response which at least looks like HTTP. We have an indicator *
2801 * of each header's length, so we can parse them quickly. *
2802 ****************************************************************/
2803
Willy Tarreau9cdde232007-05-02 20:58:19 +02002804 /* ensure we keep this pointer to the beginning of the message */
2805 msg->sol = rep->data + msg->som;
2806
Willy Tarreaua15645d2007-03-18 16:22:39 +01002807 /*
2808 * 1: get the status code and check for cacheability.
2809 */
2810
2811 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002812 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002813
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002814 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002815 case 200:
2816 case 203:
2817 case 206:
2818 case 300:
2819 case 301:
2820 case 410:
2821 /* RFC2616 @13.4:
2822 * "A response received with a status code of
2823 * 200, 203, 206, 300, 301 or 410 MAY be stored
2824 * by a cache (...) unless a cache-control
2825 * directive prohibits caching."
2826 *
2827 * RFC2616 @9.5: POST method :
2828 * "Responses to this method are not cacheable,
2829 * unless the response includes appropriate
2830 * Cache-Control or Expires header fields."
2831 */
2832 if (likely(txn->meth != HTTP_METH_POST) &&
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002833 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
Willy Tarreau3d300592007-03-18 18:34:41 +01002834 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002835 break;
2836 default:
2837 break;
2838 }
2839
2840 /*
2841 * 2: we may need to capture headers
2842 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002843 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002844 capture_headers(rep->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002845 txn->rsp.cap, t->fe->rsp_cap);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002846
2847 /*
2848 * 3: we will have to evaluate the filters.
2849 * As opposed to version 1.2, now they will be evaluated in the
2850 * filters order and not in the header order. This means that
2851 * each filter has to be validated among all headers.
2852 *
2853 * Filters are tried with ->be first, then with ->fe if it is
2854 * different from ->be.
2855 */
2856
2857 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2858
2859 cur_proxy = t->be;
2860 while (1) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002861 struct proxy *rule_set = cur_proxy;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002862
2863 /* try headers filters */
2864 if (rule_set->rsp_exp != NULL) {
2865 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2866 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002867 if (t->srv) {
2868 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002869 t->srv->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002870 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002871 cur_proxy->failed_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002872 return_srv_prx_502:
Willy Tarreaufa645582007-06-03 15:59:52 +02002873 buffer_shutr(rep);
2874 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002875 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002876 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002877 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01002878 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002879 if (!(t->flags & SN_ERR_MASK))
2880 t->flags |= SN_ERR_PRXCOND;
2881 if (!(t->flags & SN_FINST_MASK))
2882 t->flags |= SN_FINST_H;
2883 /* We used to have a free connection slot. Since we'll never use it,
2884 * we have to inform the server that it may be used by another session.
2885 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002886 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002887 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002888 return 1;
2889 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002890 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002891
Willy Tarreaua15645d2007-03-18 16:22:39 +01002892 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002893 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002894 if (t->srv) {
2895 t->srv->cur_sess--;
2896 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002897 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002898 cur_proxy->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002899 goto return_srv_prx_502;
2900 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002901
Willy Tarreaua15645d2007-03-18 16:22:39 +01002902 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002903 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01002904 !(t->flags & SN_CONN_CLOSED)) {
2905 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002906 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002907 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002908
Willy Tarreaua15645d2007-03-18 16:22:39 +01002909 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2910 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002911
Willy Tarreaua15645d2007-03-18 16:22:39 +01002912 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2913 cur_hdr = &txn->hdr_idx.v[cur_idx];
2914 cur_ptr = cur_next;
2915 cur_end = cur_ptr + cur_hdr->len;
2916 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002917
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002918 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2919 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002920 /* 3 possibilities :
2921 * - we have already set Connection: close,
2922 * so we remove this line.
2923 * - we have not yet set Connection: close,
2924 * but this line indicates close. We leave
2925 * it untouched and set the flag.
2926 * - we have not yet set Connection: close,
2927 * and this line indicates non-close. We
2928 * replace it.
2929 */
2930 if (t->flags & SN_CONN_CLOSED) {
2931 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2932 txn->rsp.eoh += delta;
2933 cur_next += delta;
2934 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2935 txn->hdr_idx.used--;
2936 cur_hdr->len = 0;
2937 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002938 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2939 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2940 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002941 cur_next += delta;
2942 cur_hdr->len += delta;
2943 txn->rsp.eoh += delta;
2944 }
2945 t->flags |= SN_CONN_CLOSED;
2946 }
2947 }
2948 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002949 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002950 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002951
Willy Tarreaua15645d2007-03-18 16:22:39 +01002952 /* add response headers from the rule sets in the same order */
2953 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002954 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2955 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002956 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002957 }
2958
Willy Tarreaua15645d2007-03-18 16:22:39 +01002959 /* check whether we're already working on the frontend */
2960 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002961 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002962 cur_proxy = t->fe;
2963 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002964
Willy Tarreaua15645d2007-03-18 16:22:39 +01002965 /*
2966 * 4: check for server cookie.
2967 */
2968 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002969
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002970
Willy Tarreaua15645d2007-03-18 16:22:39 +01002971 /*
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002972 * 5: check for cache-control or pragma headers.
2973 */
2974 check_response_for_cacheability(t, rep);
2975
2976
2977 /*
2978 * 6: add server cookie in the response if needed
Willy Tarreaua15645d2007-03-18 16:22:39 +01002979 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002980 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2981 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002982 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002983
Willy Tarreaua15645d2007-03-18 16:22:39 +01002984 /* the server is known, it's not the one the client requested, we have to
2985 * insert a set-cookie here, except if we want to insert only on POST
2986 * requests and this one isn't. Note that servers which don't have cookies
2987 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002988 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002989 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002990 t->be->cookie_name,
Willy Tarreaua15645d2007-03-18 16:22:39 +01002991 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002992
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002993 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2994 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002995 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01002996 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002997
Willy Tarreaua15645d2007-03-18 16:22:39 +01002998 /* Here, we will tell an eventual cache on the client side that we don't
2999 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3000 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3001 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3002 */
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003003 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
3004
3005 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3006
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003007 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3008 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003009 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003010 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003011 }
3012
3013
3014 /*
Willy Tarreaua15645d2007-03-18 16:22:39 +01003015 * 7: check if result will be cacheable with a cookie.
3016 * We'll block the response if security checks have caught
3017 * nasty things such as a cacheable cookie.
3018 */
Willy Tarreau3d300592007-03-18 18:34:41 +01003019 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3020 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003021 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003022
Willy Tarreaua15645d2007-03-18 16:22:39 +01003023 /* we're in presence of a cacheable response containing
3024 * a set-cookie header. We'll block it as requested by
3025 * the 'checkcache' option, and send an alert.
3026 */
3027 if (t->srv) {
3028 t->srv->cur_sess--;
3029 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003030 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003031 t->be->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003032
Willy Tarreaua15645d2007-03-18 16:22:39 +01003033 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003034 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003035 send_log(t->be, LOG_ALERT,
3036 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003037 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003038 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003039 }
3040
Willy Tarreaua15645d2007-03-18 16:22:39 +01003041 /*
3042 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02003043 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003044 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02003045 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003046 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02003047 if ((unlikely(msg->sl.st.v_l != 8) ||
3048 unlikely(req->data[msg->som + 7] != '0')) &&
3049 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003050 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003051 goto return_bad_resp;
3052 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003053 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003054
Willy Tarreaubaaee002006-06-26 02:48:02 +02003055
Willy Tarreaua15645d2007-03-18 16:22:39 +01003056 /*************************************************************
3057 * OK, that's finished for the headers. We have done what we *
3058 * could. Let's switch to the DATA state. *
3059 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003060
Willy Tarreaua15645d2007-03-18 16:22:39 +01003061 t->srv_state = SV_STDATA;
3062 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003063 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003064
3065 /* client connection already closed or option 'forceclose' required :
3066 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003067 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003068 if ((req->l == 0) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003069 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003070 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003071 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003072
3073 /* We must ensure that the read part is still alive when switching
3074 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003075 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003076 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003077
Willy Tarreaua15645d2007-03-18 16:22:39 +01003078 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003079 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003080 }
3081
Willy Tarreaua15645d2007-03-18 16:22:39 +01003082#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003083 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003084 /* TCP splicing supported by both FE and BE */
3085 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003086 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003087#endif
3088 /* if the user wants to log as soon as possible, without counting
3089 bytes from the server, then this is the right moment. */
3090 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3091 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01003092 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreau42250582007-04-01 01:30:43 +02003093 if (t->fe->to_log & LW_REQ)
3094 http_sess_log(t);
3095 else
3096 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003097 }
3098
Willy Tarreaua15645d2007-03-18 16:22:39 +01003099 /* Note: we must not try to cheat by jumping directly to DATA,
3100 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003101 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003102
3103 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003104 }
3105 else if (s == SV_STDATA) {
3106 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003107 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02003108 buffer_shutr(rep);
3109 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003110 fd_delete(t->srv_fd);
3111 if (t->srv) {
3112 t->srv->cur_sess--;
3113 t->srv->failed_resp++;
3114 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003115 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003116 t->srv_state = SV_STCLOSE;
3117 if (!(t->flags & SN_ERR_MASK))
3118 t->flags |= SN_ERR_SRVCL;
3119 if (!(t->flags & SN_FINST_MASK))
3120 t->flags |= SN_FINST_D;
3121 /* We used to have a free connection slot. Since we'll never use it,
3122 * we have to inform the server that it may be used by another session.
3123 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003124 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003125 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003126
3127 return 1;
3128 }
3129 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003130 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003131 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003132 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003133 t->srv_state = SV_STSHUTR;
3134 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3135 return 1;
3136 }
3137 /* end of client read and no more data to send */
3138 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003139 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003140 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003141 shutdown(t->srv_fd, SHUT_WR);
3142 /* We must ensure that the read part is still alive when switching
3143 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003144 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003145 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003146
3147 t->srv_state = SV_STSHUTW;
3148 return 1;
3149 }
3150 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003151 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003152 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003153 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003154 t->srv_state = SV_STSHUTR;
3155 if (!(t->flags & SN_ERR_MASK))
3156 t->flags |= SN_ERR_SRVTO;
3157 if (!(t->flags & SN_FINST_MASK))
3158 t->flags |= SN_FINST_D;
3159 return 1;
3160 }
3161 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003162 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003163 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003164 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003165 shutdown(t->srv_fd, SHUT_WR);
3166 /* We must ensure that the read part is still alive when switching
3167 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003168 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003169 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003170 t->srv_state = SV_STSHUTW;
3171 if (!(t->flags & SN_ERR_MASK))
3172 t->flags |= SN_ERR_SRVTO;
3173 if (!(t->flags & SN_FINST_MASK))
3174 t->flags |= SN_FINST_D;
3175 return 1;
3176 }
3177
3178 /* recompute request time-outs */
3179 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003180 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3181 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003182 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003183 }
3184 }
3185 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau66319382007-04-08 17:17:37 +02003186 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3187 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003188 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003189 /* FIXME: to prevent the server from expiring read timeouts during writes,
3190 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003191 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003192 }
3193 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003194 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003195 }
3196 }
3197
3198 /* recompute response time-outs */
3199 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003200 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003201 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003202 }
3203 }
3204 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003205 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003206 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003207 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003208 }
3209 }
3210
3211 return 0; /* other cases change nothing */
3212 }
3213 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003214 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003215 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003216 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003217 fd_delete(t->srv_fd);
3218 if (t->srv) {
3219 t->srv->cur_sess--;
3220 t->srv->failed_resp++;
3221 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003222 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003223 //close(t->srv_fd);
3224 t->srv_state = SV_STCLOSE;
3225 if (!(t->flags & SN_ERR_MASK))
3226 t->flags |= SN_ERR_SRVCL;
3227 if (!(t->flags & SN_FINST_MASK))
3228 t->flags |= SN_FINST_D;
3229 /* We used to have a free connection slot. Since we'll never use it,
3230 * we have to inform the server that it may be used by another session.
3231 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003232 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003233 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003234
3235 return 1;
3236 }
3237 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003238 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003239 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003240 fd_delete(t->srv_fd);
3241 if (t->srv)
3242 t->srv->cur_sess--;
3243 //close(t->srv_fd);
3244 t->srv_state = SV_STCLOSE;
3245 /* We used to have a free connection slot. Since we'll never use it,
3246 * we have to inform the server that it may be used by another session.
3247 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003248 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003249 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003250
3251 return 1;
3252 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003253 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003254 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003255 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003256 fd_delete(t->srv_fd);
3257 if (t->srv)
3258 t->srv->cur_sess--;
3259 //close(t->srv_fd);
3260 t->srv_state = SV_STCLOSE;
3261 if (!(t->flags & SN_ERR_MASK))
3262 t->flags |= SN_ERR_SRVTO;
3263 if (!(t->flags & SN_FINST_MASK))
3264 t->flags |= SN_FINST_D;
3265 /* We used to have a free connection slot. Since we'll never use it,
3266 * we have to inform the server that it may be used by another session.
3267 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003268 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003269 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003270
3271 return 1;
3272 }
3273 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003274 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3275 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003276 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003277 }
3278 }
3279 else { /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02003280 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3281 /* restart writing */
Willy Tarreau33014d02007-06-03 15:25:37 +02003282 if (!tv_add_ifset(&req->wex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003283 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003284 }
3285 }
3286 return 0;
3287 }
3288 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003289 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003290 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003291 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003292 fd_delete(t->srv_fd);
3293 if (t->srv) {
3294 t->srv->cur_sess--;
3295 t->srv->failed_resp++;
3296 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003297 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003298 //close(t->srv_fd);
3299 t->srv_state = SV_STCLOSE;
3300 if (!(t->flags & SN_ERR_MASK))
3301 t->flags |= SN_ERR_SRVCL;
3302 if (!(t->flags & SN_FINST_MASK))
3303 t->flags |= SN_FINST_D;
3304 /* We used to have a free connection slot. Since we'll never use it,
3305 * we have to inform the server that it may be used by another session.
3306 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003307 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003308 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003309
3310 return 1;
3311 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003312 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003313 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003314 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003315 fd_delete(t->srv_fd);
3316 if (t->srv)
3317 t->srv->cur_sess--;
3318 //close(t->srv_fd);
3319 t->srv_state = SV_STCLOSE;
3320 /* We used to have a free connection slot. Since we'll never use it,
3321 * we have to inform the server that it may be used by another session.
3322 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003323 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003324 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003325
3326 return 1;
3327 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003328 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003329 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003330 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003331 fd_delete(t->srv_fd);
3332 if (t->srv)
3333 t->srv->cur_sess--;
3334 //close(t->srv_fd);
3335 t->srv_state = SV_STCLOSE;
3336 if (!(t->flags & SN_ERR_MASK))
3337 t->flags |= SN_ERR_SRVTO;
3338 if (!(t->flags & SN_FINST_MASK))
3339 t->flags |= SN_FINST_D;
3340 /* We used to have a free connection slot. Since we'll never use it,
3341 * we have to inform the server that it may be used by another session.
3342 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003343 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003344 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003345
3346 return 1;
3347 }
3348 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003349 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003350 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003351 }
3352 }
3353 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003354 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003355 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003356 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003357 }
3358 }
3359 return 0;
3360 }
3361 else { /* SV_STCLOSE : nothing to do */
3362 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3363 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003364 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003365 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003366 write(1, trash, len);
3367 }
3368 return 0;
3369 }
3370 return 0;
3371}
3372
3373
3374/*
3375 * Produces data for the session <s> depending on its source. Expects to be
3376 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3377 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3378 * session, which it uses to keep on being called when there is free space in
3379 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3380 * if it changes the session state from CL_STSHUTR, otherwise 0.
3381 */
3382int produce_content(struct session *s)
3383{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003384 if (s->data_source == DATA_SRC_NONE) {
3385 s->flags &= ~SN_SELF_GEN;
3386 return 1;
3387 }
3388 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003389 /* dump server statistics */
Willy Tarreau55bb8452007-10-17 18:44:57 +02003390 int ret = stats_dump_http(s, s->be->uri_auth,
3391 (s->flags & SN_STAT_FMTCSV) ? 0 : STAT_FMT_HTML);
Willy Tarreau91861262007-10-17 17:06:05 +02003392 if (ret >= 0)
3393 return ret;
3394 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003395 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003396
Willy Tarreau91861262007-10-17 17:06:05 +02003397 /* unknown data source or internal error */
3398 s->txn.status = 500;
3399 client_retnclose(s, error_message(s, HTTP_ERR_500));
3400 if (!(s->flags & SN_ERR_MASK))
3401 s->flags |= SN_ERR_PRXCOND;
3402 if (!(s->flags & SN_FINST_MASK))
3403 s->flags |= SN_FINST_R;
3404 s->flags &= ~SN_SELF_GEN;
3405 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003406}
3407
3408
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003409/* Iterate the same filter through all request headers.
3410 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003411 * Since it can manage the switch to another backend, it updates the per-proxy
3412 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003413 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003414int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003415{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003416 char term;
3417 char *cur_ptr, *cur_end, *cur_next;
3418 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003419 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003420 struct hdr_idx_elem *cur_hdr;
3421 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003423 last_hdr = 0;
3424
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003425 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003426 old_idx = 0;
3427
3428 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003429 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003430 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003431 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003432 (exp->action == ACT_ALLOW ||
3433 exp->action == ACT_DENY ||
3434 exp->action == ACT_TARPIT))
3435 return 0;
3436
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003437 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003438 if (!cur_idx)
3439 break;
3440
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003441 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003442 cur_ptr = cur_next;
3443 cur_end = cur_ptr + cur_hdr->len;
3444 cur_next = cur_end + cur_hdr->cr + 1;
3445
3446 /* Now we have one header between cur_ptr and cur_end,
3447 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003448 */
3449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003450 /* The annoying part is that pattern matching needs
3451 * that we modify the contents to null-terminate all
3452 * strings before testing them.
3453 */
3454
3455 term = *cur_end;
3456 *cur_end = '\0';
3457
3458 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3459 switch (exp->action) {
3460 case ACT_SETBE:
3461 /* It is not possible to jump a second time.
3462 * FIXME: should we return an HTTP/500 here so that
3463 * the admin knows there's a problem ?
3464 */
3465 if (t->be != t->fe)
3466 break;
3467
3468 /* Swithing Proxy */
3469 t->be = (struct proxy *) exp->replace;
3470
3471 /* right now, the backend switch is not too much complicated
3472 * because we have associated req_cap and rsp_cap to the
3473 * frontend, and the beconn will be updated later.
3474 */
3475
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003476 t->rep->rto = t->req->wto = t->be->srvtimeout;
3477 t->req->cto = t->be->contimeout;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003478 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003479 last_hdr = 1;
3480 break;
3481
3482 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003483 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003484 last_hdr = 1;
3485 break;
3486
3487 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003488 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003489 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003490 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003491 break;
3492
3493 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003494 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003495 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003496 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003497 break;
3498
3499 case ACT_REPLACE:
3500 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3501 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3502 /* FIXME: if the user adds a newline in the replacement, the
3503 * index will not be recalculated for now, and the new line
3504 * will not be counted as a new header.
3505 */
3506
3507 cur_end += delta;
3508 cur_next += delta;
3509 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003510 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003511 break;
3512
3513 case ACT_REMOVE:
3514 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3515 cur_next += delta;
3516
3517 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003518 txn->req.eoh += delta;
3519 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3520 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003521 cur_hdr->len = 0;
3522 cur_end = NULL; /* null-term has been rewritten */
3523 break;
3524
3525 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003526 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003527 if (cur_end)
3528 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003529
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003530 /* keep the link from this header to next one in case of later
3531 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003532 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003533 old_idx = cur_idx;
3534 }
3535 return 0;
3536}
3537
3538
3539/* Apply the filter to the request line.
3540 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3541 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003542 * Since it can manage the switch to another backend, it updates the per-proxy
3543 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003544 */
3545int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3546{
3547 char term;
3548 char *cur_ptr, *cur_end;
3549 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003550 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003551 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003552
Willy Tarreau58f10d72006-12-04 02:26:12 +01003553
Willy Tarreau3d300592007-03-18 18:34:41 +01003554 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003555 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003556 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003557 (exp->action == ACT_ALLOW ||
3558 exp->action == ACT_DENY ||
3559 exp->action == ACT_TARPIT))
3560 return 0;
3561 else if (exp->action == ACT_REMOVE)
3562 return 0;
3563
3564 done = 0;
3565
Willy Tarreau9cdde232007-05-02 20:58:19 +02003566 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003567 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003568
3569 /* Now we have the request line between cur_ptr and cur_end */
3570
3571 /* The annoying part is that pattern matching needs
3572 * that we modify the contents to null-terminate all
3573 * strings before testing them.
3574 */
3575
3576 term = *cur_end;
3577 *cur_end = '\0';
3578
3579 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3580 switch (exp->action) {
3581 case ACT_SETBE:
3582 /* It is not possible to jump a second time.
3583 * FIXME: should we return an HTTP/500 here so that
3584 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003585 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003586 if (t->be != t->fe)
3587 break;
3588
3589 /* Swithing Proxy */
3590 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003591
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003592 /* right now, the backend switch is not too much complicated
3593 * because we have associated req_cap and rsp_cap to the
3594 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003595 */
3596
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003597 t->rep->rto = t->req->wto = t->be->srvtimeout;
3598 t->req->cto = t->be->contimeout;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003599 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003600 done = 1;
3601 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003603 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003604 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003605 done = 1;
3606 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003607
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003608 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003609 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003610 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003611 done = 1;
3612 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003613
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003614 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003615 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003616 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003617 done = 1;
3618 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003619
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003620 case ACT_REPLACE:
3621 *cur_end = term; /* restore the string terminator */
3622 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3623 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3624 /* FIXME: if the user adds a newline in the replacement, the
3625 * index will not be recalculated for now, and the new line
3626 * will not be counted as a new header.
3627 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003628
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003629 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003630 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003631
Willy Tarreau9cdde232007-05-02 20:58:19 +02003632 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003633 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003634 HTTP_MSG_RQMETH,
3635 cur_ptr, cur_end + 1,
3636 NULL, NULL);
3637 if (unlikely(!cur_end))
3638 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003640 /* we have a full request and we know that we have either a CR
3641 * or an LF at <ptr>.
3642 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003643 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3644 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003645 /* there is no point trying this regex on headers */
3646 return 1;
3647 }
3648 }
3649 *cur_end = term; /* restore the string terminator */
3650 return done;
3651}
Willy Tarreau97de6242006-12-27 17:18:38 +01003652
Willy Tarreau58f10d72006-12-04 02:26:12 +01003653
Willy Tarreau58f10d72006-12-04 02:26:12 +01003654
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003655/*
3656 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3657 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003658 * unparsable request. Since it can manage the switch to another backend, it
3659 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003660 */
3661int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3662{
Willy Tarreau3d300592007-03-18 18:34:41 +01003663 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003664 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003665 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003666 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003667
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003668 /*
3669 * The interleaving of transformations and verdicts
3670 * makes it difficult to decide to continue or stop
3671 * the evaluation.
3672 */
3673
Willy Tarreau3d300592007-03-18 18:34:41 +01003674 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003675 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3676 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3677 exp = exp->next;
3678 continue;
3679 }
3680
3681 /* Apply the filter to the request line. */
3682 ret = apply_filter_to_req_line(t, req, exp);
3683 if (unlikely(ret < 0))
3684 return -1;
3685
3686 if (likely(ret == 0)) {
3687 /* The filter did not match the request, it can be
3688 * iterated through all headers.
3689 */
3690 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003691 }
3692 exp = exp->next;
3693 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003694 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003695}
3696
3697
Willy Tarreaua15645d2007-03-18 16:22:39 +01003698
Willy Tarreau58f10d72006-12-04 02:26:12 +01003699/*
3700 * Manager client-side cookie
3701 */
3702void manage_client_side_cookies(struct session *t, struct buffer *req)
3703{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003704 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003705 char *p1, *p2, *p3, *p4;
3706 char *del_colon, *del_cookie, *colon;
3707 int app_cookies;
3708
3709 appsess *asession_temp = NULL;
3710 appsess local_asession;
3711
3712 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003713 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003714
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003715 if (t->be->cookie_name == NULL &&
3716 t->be->appsession_name == NULL &&
3717 t->be->capture_name == NULL)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003718 return;
3719
Willy Tarreau2a324282006-12-05 00:05:46 +01003720 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003721 * we start with the start line.
3722 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003723 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003724 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003725
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003726 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003727 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003728 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003729
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003730 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003731 cur_ptr = cur_next;
3732 cur_end = cur_ptr + cur_hdr->len;
3733 cur_next = cur_end + cur_hdr->cr + 1;
3734
3735 /* We have one full header between cur_ptr and cur_end, and the
3736 * next header starts at cur_next. We're only interested in
3737 * "Cookie:" headers.
3738 */
3739
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003740 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3741 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003742 old_idx = cur_idx;
3743 continue;
3744 }
3745
3746 /* Now look for cookies. Conforming to RFC2109, we have to support
3747 * attributes whose name begin with a '$', and associate them with
3748 * the right cookie, if we want to delete this cookie.
3749 * So there are 3 cases for each cookie read :
3750 * 1) it's a special attribute, beginning with a '$' : ignore it.
3751 * 2) it's a server id cookie that we *MAY* want to delete : save
3752 * some pointers on it (last semi-colon, beginning of cookie...)
3753 * 3) it's an application cookie : we *MAY* have to delete a previous
3754 * "special" cookie.
3755 * At the end of loop, if a "special" cookie remains, we may have to
3756 * remove it. If no application cookie persists in the header, we
3757 * *MUST* delete it
3758 */
3759
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003760 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003761
Willy Tarreau58f10d72006-12-04 02:26:12 +01003762 /* del_cookie == NULL => nothing to be deleted */
3763 del_colon = del_cookie = NULL;
3764 app_cookies = 0;
3765
3766 while (p1 < cur_end) {
3767 /* skip spaces and colons, but keep an eye on these ones */
3768 while (p1 < cur_end) {
3769 if (*p1 == ';' || *p1 == ',')
3770 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003771 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003772 break;
3773 p1++;
3774 }
3775
3776 if (p1 == cur_end)
3777 break;
3778
3779 /* p1 is at the beginning of the cookie name */
3780 p2 = p1;
3781 while (p2 < cur_end && *p2 != '=')
3782 p2++;
3783
3784 if (p2 == cur_end)
3785 break;
3786
3787 p3 = p2 + 1; /* skips the '=' sign */
3788 if (p3 == cur_end)
3789 break;
3790
3791 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003792 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003793 p4++;
3794
3795 /* here, we have the cookie name between p1 and p2,
3796 * and its value between p3 and p4.
3797 * we can process it :
3798 *
3799 * Cookie: NAME=VALUE;
3800 * | || || |
3801 * | || || +--> p4
3802 * | || |+-------> p3
3803 * | || +--------> p2
3804 * | |+------------> p1
3805 * | +-------------> colon
3806 * +--------------------> cur_ptr
3807 */
3808
3809 if (*p1 == '$') {
3810 /* skip this one */
3811 }
3812 else {
3813 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003814 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003815 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003816 (p4 - p1 >= t->fe->capture_namelen) &&
3817 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003818 int log_len = p4 - p1;
3819
Willy Tarreau086b3b42007-05-13 21:45:51 +02003820 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003821 Alert("HTTP logging : out of memory.\n");
3822 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003823 if (log_len > t->fe->capture_len)
3824 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003825 memcpy(txn->cli_cookie, p1, log_len);
3826 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003827 }
3828 }
3829
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003830 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3831 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003832 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003833 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003834 char *delim;
3835
3836 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3837 * have the server ID betweek p3 and delim, and the original cookie between
3838 * delim+1 and p4. Otherwise, delim==p4 :
3839 *
3840 * Cookie: NAME=SRV~VALUE;
3841 * | || || | |
3842 * | || || | +--> p4
3843 * | || || +--------> delim
3844 * | || |+-----------> p3
3845 * | || +------------> p2
3846 * | |+----------------> p1
3847 * | +-----------------> colon
3848 * +------------------------> cur_ptr
3849 */
3850
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003851 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003852 for (delim = p3; delim < p4; delim++)
3853 if (*delim == COOKIE_DELIM)
3854 break;
3855 }
3856 else
3857 delim = p4;
3858
3859
3860 /* Here, we'll look for the first running server which supports the cookie.
3861 * This allows to share a same cookie between several servers, for example
3862 * to dedicate backup servers to specific servers only.
3863 * However, to prevent clients from sticking to cookie-less backup server
3864 * when they have incidentely learned an empty cookie, we simply ignore
3865 * empty cookies and mark them as invalid.
3866 */
3867 if (delim == p3)
3868 srv = NULL;
3869
3870 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01003871 if (srv->cookie && (srv->cklen == delim - p3) &&
3872 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003873 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003874 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003875 txn->flags &= ~TX_CK_MASK;
3876 txn->flags |= TX_CK_VALID;
3877 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003878 t->srv = srv;
3879 break;
3880 } else {
3881 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01003882 txn->flags &= ~TX_CK_MASK;
3883 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003884 }
3885 }
3886 srv = srv->next;
3887 }
3888
Willy Tarreau3d300592007-03-18 18:34:41 +01003889 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003890 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01003891 txn->flags &= ~TX_CK_MASK;
3892 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003893 }
3894
3895 /* depending on the cookie mode, we may have to either :
3896 * - delete the complete cookie if we're in insert+indirect mode, so that
3897 * the server never sees it ;
3898 * - remove the server id from the cookie value, and tag the cookie as an
3899 * application cookie so that it does not get accidentely removed later,
3900 * if we're in cookie prefix mode
3901 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003902 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003903 int delta; /* negative */
3904
3905 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
3906 p4 += delta;
3907 cur_end += delta;
3908 cur_next += delta;
3909 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003910 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003911
3912 del_cookie = del_colon = NULL;
3913 app_cookies++; /* protect the header from deletion */
3914 }
3915 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003916 (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 +01003917 del_cookie = p1;
3918 del_colon = colon;
3919 }
3920 } else {
3921 /* now we know that we must keep this cookie since it's
3922 * not ours. But if we wanted to delete our cookie
3923 * earlier, we cannot remove the complete header, but we
3924 * can remove the previous block itself.
3925 */
3926 app_cookies++;
3927
3928 if (del_cookie != NULL) {
3929 int delta; /* negative */
3930
3931 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
3932 p4 += delta;
3933 cur_end += delta;
3934 cur_next += delta;
3935 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003936 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003937 del_cookie = del_colon = NULL;
3938 }
3939 }
3940
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003941 if ((t->be->appsession_name != NULL) &&
3942 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003943 /* first, let's see if the cookie is our appcookie*/
3944
3945 /* Cool... it's the right one */
3946
3947 asession_temp = &local_asession;
3948
Willy Tarreau63963c62007-05-13 21:29:55 +02003949 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003950 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3951 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3952 return;
3953 }
3954
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003955 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
3956 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003957 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02003958
Willy Tarreau58f10d72006-12-04 02:26:12 +01003959 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02003960 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
3961 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02003962 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003963 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003964 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003965 Alert("Not enough memory process_cli():asession:calloc().\n");
3966 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
3967 return;
3968 }
3969
3970 asession_temp->sessid = local_asession.sessid;
3971 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02003972 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003973 } else {
3974 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003975 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003976 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003977 if (asession_temp->serverid == NULL) {
3978 Alert("Found Application Session without matching server.\n");
3979 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003980 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003981 while (srv) {
3982 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003983 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003984 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003985 txn->flags &= ~TX_CK_MASK;
3986 txn->flags |= TX_CK_VALID;
3987 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003988 t->srv = srv;
3989 break;
3990 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01003991 txn->flags &= ~TX_CK_MASK;
3992 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003993 }
3994 }
3995 srv = srv->next;
3996 }/* end while(srv) */
3997 }/* end else if server == NULL */
3998
Willy Tarreaud825eef2007-05-12 22:35:00 +02003999 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004000 }/* end if ((t->proxy->appsession_name != NULL) ... */
4001 }
4002
4003 /* we'll have to look for another cookie ... */
4004 p1 = p4;
4005 } /* while (p1 < cur_end) */
4006
4007 /* There's no more cookie on this line.
4008 * We may have marked the last one(s) for deletion.
4009 * We must do this now in two ways :
4010 * - if there is no app cookie, we simply delete the header ;
4011 * - if there are app cookies, we must delete the end of the
4012 * string properly, including the colon/semi-colon before
4013 * the cookie name.
4014 */
4015 if (del_cookie != NULL) {
4016 int delta;
4017 if (app_cookies) {
4018 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4019 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004020 cur_hdr->len += delta;
4021 } else {
4022 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004023
4024 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004025 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4026 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004027 cur_hdr->len = 0;
4028 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004029 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004030 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004031 }
4032
4033 /* keep the link from this header to next one */
4034 old_idx = cur_idx;
4035 } /* end of cookie processing on this header */
4036}
4037
4038
Willy Tarreaua15645d2007-03-18 16:22:39 +01004039/* Iterate the same filter through all response headers contained in <rtr>.
4040 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4041 */
4042int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4043{
4044 char term;
4045 char *cur_ptr, *cur_end, *cur_next;
4046 int cur_idx, old_idx, last_hdr;
4047 struct http_txn *txn = &t->txn;
4048 struct hdr_idx_elem *cur_hdr;
4049 int len, delta;
4050
4051 last_hdr = 0;
4052
4053 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4054 old_idx = 0;
4055
4056 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004057 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004058 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004059 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004060 (exp->action == ACT_ALLOW ||
4061 exp->action == ACT_DENY))
4062 return 0;
4063
4064 cur_idx = txn->hdr_idx.v[old_idx].next;
4065 if (!cur_idx)
4066 break;
4067
4068 cur_hdr = &txn->hdr_idx.v[cur_idx];
4069 cur_ptr = cur_next;
4070 cur_end = cur_ptr + cur_hdr->len;
4071 cur_next = cur_end + cur_hdr->cr + 1;
4072
4073 /* Now we have one header between cur_ptr and cur_end,
4074 * and the next header starts at cur_next.
4075 */
4076
4077 /* The annoying part is that pattern matching needs
4078 * that we modify the contents to null-terminate all
4079 * strings before testing them.
4080 */
4081
4082 term = *cur_end;
4083 *cur_end = '\0';
4084
4085 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4086 switch (exp->action) {
4087 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004088 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004089 last_hdr = 1;
4090 break;
4091
4092 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004093 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004094 last_hdr = 1;
4095 break;
4096
4097 case ACT_REPLACE:
4098 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4099 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4100 /* FIXME: if the user adds a newline in the replacement, the
4101 * index will not be recalculated for now, and the new line
4102 * will not be counted as a new header.
4103 */
4104
4105 cur_end += delta;
4106 cur_next += delta;
4107 cur_hdr->len += delta;
4108 txn->rsp.eoh += delta;
4109 break;
4110
4111 case ACT_REMOVE:
4112 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4113 cur_next += delta;
4114
4115 /* FIXME: this should be a separate function */
4116 txn->rsp.eoh += delta;
4117 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4118 txn->hdr_idx.used--;
4119 cur_hdr->len = 0;
4120 cur_end = NULL; /* null-term has been rewritten */
4121 break;
4122
4123 }
4124 }
4125 if (cur_end)
4126 *cur_end = term; /* restore the string terminator */
4127
4128 /* keep the link from this header to next one in case of later
4129 * removal of next header.
4130 */
4131 old_idx = cur_idx;
4132 }
4133 return 0;
4134}
4135
4136
4137/* Apply the filter to the status line in the response buffer <rtr>.
4138 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4139 * or -1 if a replacement resulted in an invalid status line.
4140 */
4141int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4142{
4143 char term;
4144 char *cur_ptr, *cur_end;
4145 int done;
4146 struct http_txn *txn = &t->txn;
4147 int len, delta;
4148
4149
Willy Tarreau3d300592007-03-18 18:34:41 +01004150 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004151 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004152 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004153 (exp->action == ACT_ALLOW ||
4154 exp->action == ACT_DENY))
4155 return 0;
4156 else if (exp->action == ACT_REMOVE)
4157 return 0;
4158
4159 done = 0;
4160
Willy Tarreau9cdde232007-05-02 20:58:19 +02004161 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004162 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4163
4164 /* Now we have the status line between cur_ptr and cur_end */
4165
4166 /* The annoying part is that pattern matching needs
4167 * that we modify the contents to null-terminate all
4168 * strings before testing them.
4169 */
4170
4171 term = *cur_end;
4172 *cur_end = '\0';
4173
4174 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4175 switch (exp->action) {
4176 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004177 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004178 done = 1;
4179 break;
4180
4181 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004182 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004183 done = 1;
4184 break;
4185
4186 case ACT_REPLACE:
4187 *cur_end = term; /* restore the string terminator */
4188 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4189 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4190 /* FIXME: if the user adds a newline in the replacement, the
4191 * index will not be recalculated for now, and the new line
4192 * will not be counted as a new header.
4193 */
4194
4195 txn->rsp.eoh += delta;
4196 cur_end += delta;
4197
Willy Tarreau9cdde232007-05-02 20:58:19 +02004198 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004199 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004200 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004201 cur_ptr, cur_end + 1,
4202 NULL, NULL);
4203 if (unlikely(!cur_end))
4204 return -1;
4205
4206 /* we have a full respnse and we know that we have either a CR
4207 * or an LF at <ptr>.
4208 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004209 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004210 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4211 /* there is no point trying this regex on headers */
4212 return 1;
4213 }
4214 }
4215 *cur_end = term; /* restore the string terminator */
4216 return done;
4217}
4218
4219
4220
4221/*
4222 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4223 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4224 * unparsable response.
4225 */
4226int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4227{
Willy Tarreau3d300592007-03-18 18:34:41 +01004228 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004229 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004230 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004231 int ret;
4232
4233 /*
4234 * The interleaving of transformations and verdicts
4235 * makes it difficult to decide to continue or stop
4236 * the evaluation.
4237 */
4238
Willy Tarreau3d300592007-03-18 18:34:41 +01004239 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004240 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4241 exp->action == ACT_PASS)) {
4242 exp = exp->next;
4243 continue;
4244 }
4245
4246 /* Apply the filter to the status line. */
4247 ret = apply_filter_to_sts_line(t, rtr, exp);
4248 if (unlikely(ret < 0))
4249 return -1;
4250
4251 if (likely(ret == 0)) {
4252 /* The filter did not match the response, it can be
4253 * iterated through all headers.
4254 */
4255 apply_filter_to_resp_headers(t, rtr, exp);
4256 }
4257 exp = exp->next;
4258 }
4259 return 0;
4260}
4261
4262
4263
4264/*
4265 * Manager server-side cookies
4266 */
4267void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4268{
4269 struct http_txn *txn = &t->txn;
4270 char *p1, *p2, *p3, *p4;
4271
4272 appsess *asession_temp = NULL;
4273 appsess local_asession;
4274
4275 char *cur_ptr, *cur_end, *cur_next;
4276 int cur_idx, old_idx, delta;
4277
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004278 if (t->be->cookie_name == NULL &&
4279 t->be->appsession_name == NULL &&
4280 t->be->capture_name == NULL &&
4281 !(t->be->options & PR_O_CHK_CACHE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004282 return;
4283
4284 /* Iterate through the headers.
4285 * we start with the start line.
4286 */
4287 old_idx = 0;
4288 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4289
4290 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4291 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004292 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004293
4294 cur_hdr = &txn->hdr_idx.v[cur_idx];
4295 cur_ptr = cur_next;
4296 cur_end = cur_ptr + cur_hdr->len;
4297 cur_next = cur_end + cur_hdr->cr + 1;
4298
4299 /* We have one full header between cur_ptr and cur_end, and the
4300 * next header starts at cur_next. We're only interested in
4301 * "Cookie:" headers.
4302 */
4303
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004304 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4305 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004306 old_idx = cur_idx;
4307 continue;
4308 }
4309
4310 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004311 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004312
4313
4314 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004315 if (t->be->cookie_name == NULL &&
4316 t->be->appsession_name == NULL &&
4317 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004318 return;
4319
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004320 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004321
4322 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004323 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4324 break;
4325
4326 /* p1 is at the beginning of the cookie name */
4327 p2 = p1;
4328
4329 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4330 p2++;
4331
4332 if (p2 == cur_end || *p2 == ';') /* next cookie */
4333 break;
4334
4335 p3 = p2 + 1; /* skip the '=' sign */
4336 if (p3 == cur_end)
4337 break;
4338
4339 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004340 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004341 p4++;
4342
4343 /* here, we have the cookie name between p1 and p2,
4344 * and its value between p3 and p4.
4345 * we can process it.
4346 */
4347
4348 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004349 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004350 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004351 (p4 - p1 >= t->be->capture_namelen) &&
4352 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004353 int log_len = p4 - p1;
4354
Willy Tarreau086b3b42007-05-13 21:45:51 +02004355 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004356 Alert("HTTP logging : out of memory.\n");
4357 }
4358
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004359 if (log_len > t->be->capture_len)
4360 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004361 memcpy(txn->srv_cookie, p1, log_len);
4362 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004363 }
4364
4365 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004366 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4367 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004368 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004369 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004370
4371 /* If the cookie is in insert mode on a known server, we'll delete
4372 * this occurrence because we'll insert another one later.
4373 * We'll delete it too if the "indirect" option is set and we're in
4374 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004375 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4376 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004377 /* this header must be deleted */
4378 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4379 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4380 txn->hdr_idx.used--;
4381 cur_hdr->len = 0;
4382 cur_next += delta;
4383 txn->rsp.eoh += delta;
4384
Willy Tarreau3d300592007-03-18 18:34:41 +01004385 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004386 }
4387 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004388 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004389 /* replace bytes p3->p4 with the cookie name associated
4390 * with this server since we know it.
4391 */
4392 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4393 cur_hdr->len += delta;
4394 cur_next += delta;
4395 txn->rsp.eoh += delta;
4396
Willy Tarreau3d300592007-03-18 18:34:41 +01004397 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004398 }
4399 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004400 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004401 /* insert the cookie name associated with this server
4402 * before existing cookie, and insert a delimitor between them..
4403 */
4404 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4405 cur_hdr->len += delta;
4406 cur_next += delta;
4407 txn->rsp.eoh += delta;
4408
4409 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004410 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004411 }
4412 }
4413 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004414 else if ((t->be->appsession_name != NULL) &&
4415 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004416
4417 /* Cool... it's the right one */
4418
4419 size_t server_id_len = strlen(t->srv->id) + 1;
4420 asession_temp = &local_asession;
4421
Willy Tarreau63963c62007-05-13 21:29:55 +02004422 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004423 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4424 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4425 return;
4426 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004427 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4428 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004429 asession_temp->serverid = NULL;
4430
4431 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004432 if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004433 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004434 Alert("Not enough Memory process_srv():asession:calloc().\n");
4435 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4436 return;
4437 }
4438 asession_temp->sessid = local_asession.sessid;
4439 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02004440 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4441 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004442 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004443 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004444 }
4445
Willy Tarreaua15645d2007-03-18 16:22:39 +01004446 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004447 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004448 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4449 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4450 return;
4451 }
4452 asession_temp->serverid[0] = '\0';
4453 }
4454
4455 if (asession_temp->serverid[0] == '\0')
4456 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4457
Willy Tarreaud825eef2007-05-12 22:35:00 +02004458 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004459
4460#if defined(DEBUG_HASH)
Willy Tarreau51041c72007-09-09 21:56:53 +02004461 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004462#endif
4463 }/* end if ((t->proxy->appsession_name != NULL) ... */
4464 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4465 } /* we're now at the end of the cookie value */
4466
4467 /* keep the link from this header to next one */
4468 old_idx = cur_idx;
4469 } /* end of cookie processing on this header */
4470}
4471
4472
4473
4474/*
4475 * Check if response is cacheable or not. Updates t->flags.
4476 */
4477void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4478{
4479 struct http_txn *txn = &t->txn;
4480 char *p1, *p2;
4481
4482 char *cur_ptr, *cur_end, *cur_next;
4483 int cur_idx;
4484
Willy Tarreau3d300592007-03-18 18:34:41 +01004485 if (!txn->flags & TX_CACHEABLE)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004486 return;
4487
4488 /* Iterate through the headers.
4489 * we start with the start line.
4490 */
4491 cur_idx = 0;
4492 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4493
4494 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4495 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004496 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004497
4498 cur_hdr = &txn->hdr_idx.v[cur_idx];
4499 cur_ptr = cur_next;
4500 cur_end = cur_ptr + cur_hdr->len;
4501 cur_next = cur_end + cur_hdr->cr + 1;
4502
4503 /* We have one full header between cur_ptr and cur_end, and the
4504 * next header starts at cur_next. We're only interested in
4505 * "Cookie:" headers.
4506 */
4507
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004508 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4509 if (val) {
4510 if ((cur_end - (cur_ptr + val) >= 8) &&
4511 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4512 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4513 return;
4514 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004515 }
4516
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004517 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4518 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004519 continue;
4520
4521 /* OK, right now we know we have a cache-control header at cur_ptr */
4522
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004523 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004524
4525 if (p1 >= cur_end) /* no more info */
4526 continue;
4527
4528 /* p1 is at the beginning of the value */
4529 p2 = p1;
4530
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004531 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004532 p2++;
4533
4534 /* we have a complete value between p1 and p2 */
4535 if (p2 < cur_end && *p2 == '=') {
4536 /* we have something of the form no-cache="set-cookie" */
4537 if ((cur_end - p1 >= 21) &&
4538 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4539 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004540 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004541 continue;
4542 }
4543
4544 /* OK, so we know that either p2 points to the end of string or to a comma */
4545 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4546 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4547 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4548 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004549 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004550 return;
4551 }
4552
4553 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004554 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004555 continue;
4556 }
4557 }
4558}
4559
4560
Willy Tarreau58f10d72006-12-04 02:26:12 +01004561/*
4562 * Try to retrieve a known appsession in the URI, then the associated server.
4563 * If the server is found, it's assigned to the session.
4564 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004565void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004566{
Willy Tarreau3d300592007-03-18 18:34:41 +01004567 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004568 appsess *asession_temp = NULL;
4569 appsess local_asession;
4570 char *request_line;
4571
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004572 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004573 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004574 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004575 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004576 return;
4577
4578 /* skip ';' */
4579 request_line++;
4580
4581 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004582 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004583 return;
4584
4585 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004586 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004587
4588 /* First try if we already have an appsession */
4589 asession_temp = &local_asession;
4590
Willy Tarreau63963c62007-05-13 21:29:55 +02004591 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004592 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4593 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4594 return;
4595 }
4596
4597 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004598 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4599 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004600 asession_temp->serverid = NULL;
4601
4602 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004603 if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004604 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004605 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004606 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004607 Alert("Not enough memory process_cli():asession:calloc().\n");
4608 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4609 return;
4610 }
4611 asession_temp->sessid = local_asession.sessid;
4612 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02004613 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004614 }
4615 else {
4616 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004617 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004618 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004619
Willy Tarreaud825eef2007-05-12 22:35:00 +02004620 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004621 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004622
Willy Tarreau58f10d72006-12-04 02:26:12 +01004623#if defined(DEBUG_HASH)
Willy Tarreau51041c72007-09-09 21:56:53 +02004624 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004625#endif
4626 if (asession_temp->serverid == NULL) {
4627 Alert("Found Application Session without matching server.\n");
4628 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004629 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004630 while (srv) {
4631 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004632 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004633 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004634 txn->flags &= ~TX_CK_MASK;
4635 txn->flags |= TX_CK_VALID;
4636 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004637 t->srv = srv;
4638 break;
4639 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004640 txn->flags &= ~TX_CK_MASK;
4641 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004642 }
4643 }
4644 srv = srv->next;
4645 }
4646 }
4647}
4648
4649
Willy Tarreaub2513902006-12-17 14:52:38 +01004650/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004651 * In a GET or HEAD request, check if the requested URI matches the stats uri
4652 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004653 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004654 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004655 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004656 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004657 *
4658 * Returns 1 if the session's state changes, otherwise 0.
4659 */
4660int stats_check_uri_auth(struct session *t, struct proxy *backend)
4661{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004662 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004663 struct uri_auth *uri_auth = backend->uri_auth;
4664 struct user_auth *user;
4665 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004666 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004667
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004668 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004669 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004670 return 0;
4671
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004672 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004673
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004674 /* the URI is in h */
4675 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004676 return 0;
4677
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004678 h += uri_auth->uri_len;
4679 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4680 if (memcmp(h, ";up", 3) == 0) {
4681 t->flags |= SN_STAT_HIDEDWN;
4682 break;
4683 }
4684 h++;
4685 }
4686
4687 if (uri_auth->refresh) {
4688 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4689 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4690 if (memcmp(h, ";norefresh", 10) == 0) {
4691 t->flags |= SN_STAT_NORFRSH;
4692 break;
4693 }
4694 h++;
4695 }
4696 }
4697
Willy Tarreau55bb8452007-10-17 18:44:57 +02004698 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4699 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4700 if (memcmp(h, ";csv", 4) == 0) {
4701 t->flags |= SN_STAT_FMTCSV;
4702 break;
4703 }
4704 h++;
4705 }
4706
Willy Tarreaub2513902006-12-17 14:52:38 +01004707 /* we are in front of a interceptable URI. Let's check
4708 * if there's an authentication and if it's valid.
4709 */
4710 user = uri_auth->users;
4711 if (!user) {
4712 /* no user auth required, it's OK */
4713 authenticated = 1;
4714 } else {
4715 authenticated = 0;
4716
4717 /* a user list is defined, we have to check.
4718 * skip 21 chars for "Authorization: Basic ".
4719 */
4720
4721 /* FIXME: this should move to an earlier place */
4722 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004723 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4724 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4725 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004726 if (len > 14 &&
4727 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004728 txn->auth_hdr.str = h;
4729 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004730 break;
4731 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004732 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004733 }
4734
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004735 if (txn->auth_hdr.len < 21 ||
4736 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004737 user = NULL;
4738
4739 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004740 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4741 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004742 user->user_pwd, user->user_len)) {
4743 authenticated = 1;
4744 break;
4745 }
4746 user = user->next;
4747 }
4748 }
4749
4750 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004751 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004752
4753 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004754 msg.str = trash;
4755 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004756 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01004757 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01004758 if (!(t->flags & SN_ERR_MASK))
4759 t->flags |= SN_ERR_PRXCOND;
4760 if (!(t->flags & SN_FINST_MASK))
4761 t->flags |= SN_FINST_R;
4762 return 1;
4763 }
4764
4765 /* The request is valid, the user is authenticate. Let's start sending
4766 * data.
4767 */
4768 t->cli_state = CL_STSHUTR;
4769 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02004770 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub2513902006-12-17 14:52:38 +01004771 t->data_source = DATA_SRC_STATS;
4772 t->data_state = DATA_ST_INIT;
4773 produce_content(t);
4774 return 1;
4775}
4776
4777
Willy Tarreaubaaee002006-06-26 02:48:02 +02004778/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004779 * Print a debug line with a header
4780 */
4781void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4782{
4783 int len, max;
4784 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
4785 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
4786 max = end - start;
4787 UBOUND(max, sizeof(trash) - len - 1);
4788 len += strlcpy2(trash + len, start, max + 1);
4789 trash[len++] = '\n';
4790 write(1, trash, len);
4791}
4792
4793
Willy Tarreau8797c062007-05-07 00:55:35 +02004794/************************************************************************/
4795/* The code below is dedicated to ACL parsing and matching */
4796/************************************************************************/
4797
4798
4799
4800
4801/* 1. Check on METHOD
4802 * We use the pre-parsed method if it is known, and store its number as an
4803 * integer. If it is unknown, we use the pointer and the length.
4804 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004805static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004806{
4807 int len, meth;
4808
Willy Tarreauae8b7962007-06-09 23:10:04 +02004809 len = strlen(*text);
4810 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004811
4812 pattern->val.i = meth;
4813 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004814 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004815 if (!pattern->ptr.str)
4816 return 0;
4817 pattern->len = len;
4818 }
4819 return 1;
4820}
4821
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004822static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004823acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4824 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004825{
4826 int meth;
4827 struct http_txn *txn = l7;
4828
Willy Tarreauc11416f2007-06-17 16:58:38 +02004829 if (txn->req.msg_state != HTTP_MSG_BODY)
4830 return 0;
4831
Willy Tarreau8797c062007-05-07 00:55:35 +02004832 meth = txn->meth;
4833 test->i = meth;
4834 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004835 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4836 /* ensure the indexes are not affected */
4837 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004838 test->len = txn->req.sl.rq.m_l;
4839 test->ptr = txn->req.sol;
4840 }
4841 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4842 return 1;
4843}
4844
4845static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4846{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004847 int icase;
4848
Willy Tarreau8797c062007-05-07 00:55:35 +02004849 if (test->i != pattern->val.i)
4850 return 0;
4851
4852 if (test->i != HTTP_METH_OTHER)
4853 return 1;
4854
4855 /* Other method, we must compare the strings */
4856 if (pattern->len != test->len)
4857 return 0;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004858
4859 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4860 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4861 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau8797c062007-05-07 00:55:35 +02004862 return 0;
4863 return 1;
4864}
4865
4866/* 2. Check on Request/Status Version
4867 * We simply compare strings here.
4868 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004869static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004870{
Willy Tarreauae8b7962007-06-09 23:10:04 +02004871 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004872 if (!pattern->ptr.str)
4873 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02004874 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004875 return 1;
4876}
4877
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004878static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004879acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
4880 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004881{
4882 struct http_txn *txn = l7;
4883 char *ptr;
4884 int len;
4885
Willy Tarreauc11416f2007-06-17 16:58:38 +02004886 if (txn->req.msg_state != HTTP_MSG_BODY)
4887 return 0;
4888
Willy Tarreau8797c062007-05-07 00:55:35 +02004889 len = txn->req.sl.rq.v_l;
4890 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
4891
4892 while ((len-- > 0) && (*ptr++ != '/'));
4893 if (len <= 0)
4894 return 0;
4895
4896 test->ptr = ptr;
4897 test->len = len;
4898
4899 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4900 return 1;
4901}
4902
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004903static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004904acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
4905 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004906{
4907 struct http_txn *txn = l7;
4908 char *ptr;
4909 int len;
4910
Willy Tarreauc11416f2007-06-17 16:58:38 +02004911 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4912 return 0;
4913
Willy Tarreau8797c062007-05-07 00:55:35 +02004914 len = txn->rsp.sl.st.v_l;
4915 ptr = txn->rsp.sol;
4916
4917 while ((len-- > 0) && (*ptr++ != '/'));
4918 if (len <= 0)
4919 return 0;
4920
4921 test->ptr = ptr;
4922 test->len = len;
4923
4924 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4925 return 1;
4926}
4927
4928/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004929static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004930acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
4931 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004932{
4933 struct http_txn *txn = l7;
4934 char *ptr;
4935 int len;
4936
Willy Tarreauc11416f2007-06-17 16:58:38 +02004937 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4938 return 0;
4939
Willy Tarreau8797c062007-05-07 00:55:35 +02004940 len = txn->rsp.sl.st.c_l;
4941 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
4942
4943 test->i = __strl2ui(ptr, len);
4944 test->flags = ACL_TEST_F_VOL_1ST;
4945 return 1;
4946}
4947
4948/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004949static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004950acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
4951 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004952{
4953 struct http_txn *txn = l7;
4954
Willy Tarreauc11416f2007-06-17 16:58:38 +02004955 if (txn->req.msg_state != HTTP_MSG_BODY)
4956 return 0;
4957 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4958 /* ensure the indexes are not affected */
4959 return 0;
4960
Willy Tarreau8797c062007-05-07 00:55:35 +02004961 test->len = txn->req.sl.rq.u_l;
4962 test->ptr = txn->req.sol + txn->req.sl.rq.u;
4963
Willy Tarreauf3d25982007-05-08 22:45:09 +02004964 /* we do not need to set READ_ONLY because the data is in a buffer */
4965 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02004966 return 1;
4967}
4968
Willy Tarreauc11416f2007-06-17 16:58:38 +02004969/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
4970 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
4971 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02004972static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004973acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004974 struct acl_expr *expr, struct acl_test *test)
4975{
4976 struct http_txn *txn = l7;
4977 struct hdr_idx *idx = &txn->hdr_idx;
4978 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004979
4980 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4981 /* search for header from the beginning */
4982 ctx->idx = 0;
4983
Willy Tarreau33a7e692007-06-10 19:45:56 +02004984 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4985 test->flags |= ACL_TEST_F_FETCH_MORE;
4986 test->flags |= ACL_TEST_F_VOL_HDR;
4987 test->len = ctx->vlen;
4988 test->ptr = (char *)ctx->line + ctx->val;
4989 return 1;
4990 }
4991
4992 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4993 test->flags |= ACL_TEST_F_VOL_HDR;
4994 return 0;
4995}
4996
Willy Tarreau33a7e692007-06-10 19:45:56 +02004997static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004998acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
4999 struct acl_expr *expr, struct acl_test *test)
5000{
5001 struct http_txn *txn = l7;
5002
5003 if (txn->req.msg_state != HTTP_MSG_BODY)
5004 return 0;
5005 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5006 /* ensure the indexes are not affected */
5007 return 0;
5008
5009 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5010}
5011
5012static int
5013acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5014 struct acl_expr *expr, struct acl_test *test)
5015{
5016 struct http_txn *txn = l7;
5017
5018 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5019 return 0;
5020
5021 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5022}
5023
5024/* 6. Check on HTTP header count. The number of occurrences is returned.
5025 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5026 */
5027static int
5028acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005029 struct acl_expr *expr, struct acl_test *test)
5030{
5031 struct http_txn *txn = l7;
5032 struct hdr_idx *idx = &txn->hdr_idx;
5033 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005034 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005035
Willy Tarreau33a7e692007-06-10 19:45:56 +02005036 ctx.idx = 0;
5037 cnt = 0;
5038 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5039 cnt++;
5040
5041 test->i = cnt;
5042 test->flags = ACL_TEST_F_VOL_HDR;
5043 return 1;
5044}
5045
Willy Tarreauc11416f2007-06-17 16:58:38 +02005046static int
5047acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5048 struct acl_expr *expr, struct acl_test *test)
5049{
5050 struct http_txn *txn = l7;
5051
5052 if (txn->req.msg_state != HTTP_MSG_BODY)
5053 return 0;
5054 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5055 /* ensure the indexes are not affected */
5056 return 0;
5057
5058 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5059}
5060
5061static int
5062acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5063 struct acl_expr *expr, struct acl_test *test)
5064{
5065 struct http_txn *txn = l7;
5066
5067 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5068 return 0;
5069
5070 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5071}
5072
Willy Tarreau33a7e692007-06-10 19:45:56 +02005073/* 7. Check on HTTP header's integer value. The integer value is returned.
5074 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005075 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005076 */
5077static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005078acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005079 struct acl_expr *expr, struct acl_test *test)
5080{
5081 struct http_txn *txn = l7;
5082 struct hdr_idx *idx = &txn->hdr_idx;
5083 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005084
5085 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5086 /* search for header from the beginning */
5087 ctx->idx = 0;
5088
Willy Tarreau33a7e692007-06-10 19:45:56 +02005089 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5090 test->flags |= ACL_TEST_F_FETCH_MORE;
5091 test->flags |= ACL_TEST_F_VOL_HDR;
5092 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5093 return 1;
5094 }
5095
5096 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5097 test->flags |= ACL_TEST_F_VOL_HDR;
5098 return 0;
5099}
5100
Willy Tarreauc11416f2007-06-17 16:58:38 +02005101static int
5102acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5103 struct acl_expr *expr, struct acl_test *test)
5104{
5105 struct http_txn *txn = l7;
5106
5107 if (txn->req.msg_state != HTTP_MSG_BODY)
5108 return 0;
5109 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5110 /* ensure the indexes are not affected */
5111 return 0;
5112
5113 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5114}
5115
5116static int
5117acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5118 struct acl_expr *expr, struct acl_test *test)
5119{
5120 struct http_txn *txn = l7;
5121
5122 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5123 return 0;
5124
5125 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5126}
5127
Willy Tarreau737b0c12007-06-10 21:28:46 +02005128/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5129 * the first '/' after the possible hostname, and ends before the possible '?'.
5130 */
5131static int
5132acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5133 struct acl_expr *expr, struct acl_test *test)
5134{
5135 struct http_txn *txn = l7;
5136 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005137
Willy Tarreauc11416f2007-06-17 16:58:38 +02005138 if (txn->req.msg_state != HTTP_MSG_BODY)
5139 return 0;
5140 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5141 /* ensure the indexes are not affected */
5142 return 0;
5143
Willy Tarreau737b0c12007-06-10 21:28:46 +02005144 ptr = txn->req.sol + txn->req.sl.rq.u;
5145 end = ptr + txn->req.sl.rq.u_l;
5146
5147 if (ptr >= end)
5148 return 0;
5149
5150 /* RFC2616, par. 5.1.2 :
5151 * Request-URI = "*" | absuri | abspath | authority
5152 */
5153
5154 if (*ptr == '*')
5155 return 0;
5156
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005157 if (isalpha((unsigned char)*ptr)) {
Willy Tarreau737b0c12007-06-10 21:28:46 +02005158 /* this is a scheme as described by RFC3986, par. 3.1 */
5159 ptr++;
5160 while (ptr < end &&
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005161 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
Willy Tarreau737b0c12007-06-10 21:28:46 +02005162 ptr++;
5163 /* skip '://' */
5164 if (ptr == end || *ptr++ != ':')
5165 return 0;
5166 if (ptr == end || *ptr++ != '/')
5167 return 0;
5168 if (ptr == end || *ptr++ != '/')
5169 return 0;
5170 }
5171 /* skip [user[:passwd]@]host[:[port]] */
5172
5173 while (ptr < end && *ptr != '/')
5174 ptr++;
5175
5176 if (ptr == end)
5177 return 0;
5178
5179 /* OK, we got the '/' ! */
5180 test->ptr = ptr;
5181
5182 while (ptr < end && *ptr != '?')
5183 ptr++;
5184
5185 test->len = ptr - test->ptr;
5186
5187 /* we do not need to set READ_ONLY because the data is in a buffer */
5188 test->flags = ACL_TEST_F_VOL_1ST;
5189 return 1;
5190}
5191
5192
Willy Tarreau8797c062007-05-07 00:55:35 +02005193
5194/************************************************************************/
5195/* All supported keywords must be declared here. */
5196/************************************************************************/
5197
5198/* Note: must not be declared <const> as its list will be overwritten */
5199static struct acl_kw_list acl_kws = {{ },{
5200 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth },
5201 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str },
5202 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str },
Willy Tarreauae8b7962007-06-09 23:10:04 +02005203 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int },
Willy Tarreau8797c062007-05-07 00:55:35 +02005204
Willy Tarreauae8b7962007-06-09 23:10:04 +02005205 { "url", acl_parse_str, acl_fetch_url, acl_match_str },
5206 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg },
5207 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end },
5208 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub },
5209 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir },
5210 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom },
5211 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg },
Willy Tarreau8797c062007-05-07 00:55:35 +02005212
Willy Tarreauc11416f2007-06-17 16:58:38 +02005213 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str },
5214 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg },
5215 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg },
5216 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end },
5217 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub },
5218 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir },
5219 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom },
5220 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int },
5221 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int },
5222
5223 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str },
5224 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg },
5225 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg },
5226 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end },
5227 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub },
5228 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir },
5229 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom },
5230 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int },
5231 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005232
5233 { "path", acl_parse_str, acl_fetch_path, acl_match_str },
5234 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg },
5235 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg },
5236 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end },
5237 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub },
5238 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir },
5239 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom },
5240
Willy Tarreauf3d25982007-05-08 22:45:09 +02005241 { NULL, NULL, NULL, NULL },
5242
5243#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005244 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5245 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5246 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5247 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5248 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5249 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5250 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5251
Willy Tarreau8797c062007-05-07 00:55:35 +02005252 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5253 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5254 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5255 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5256 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5257 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5258 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5259 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5260
5261 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5262 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5263 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5264 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5265 { NULL, NULL, NULL, NULL },
5266#endif
5267}};
5268
5269
5270__attribute__((constructor))
5271static void __http_protocol_init(void)
5272{
5273 acl_register_keywords(&acl_kws);
5274}
5275
5276
Willy Tarreau58f10d72006-12-04 02:26:12 +01005277/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005278 * Local variables:
5279 * c-indent-level: 8
5280 * c-basic-offset: 8
5281 * End:
5282 */