blob: 99bef1b2ce64d59265b902092067da8f26093c91 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaua15645d2007-03-18 16:22:39 +01004 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
33#include <common/time.h>
34#include <common/uri_auth.h>
35#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
Willy Tarreau8797c062007-05-07 00:55:35 +020037#include <types/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <types/capture.h>
39#include <types/client.h>
40#include <types/global.h>
41#include <types/httperr.h>
42#include <types/polling.h>
43#include <types/proxy.h>
44#include <types/server.h>
45
Willy Tarreau8797c062007-05-07 00:55:35 +020046#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/backend.h>
48#include <proto/buffers.h>
Willy Tarreau91861262007-10-17 17:06:05 +020049#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/fd.h>
51#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010052#include <proto/hdr_idx.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/proto_http.h>
54#include <proto/queue.h>
Willy Tarreau91861262007-10-17 17:06:05 +020055#include <proto/senddata.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/session.h>
57#include <proto/task.h>
58
Willy Tarreau6d1a9882007-01-07 02:03:04 +010059#ifdef CONFIG_HAP_TCPSPLICE
60#include <libtcpsplice.h>
61#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020062
Willy Tarreau58f10d72006-12-04 02:26:12 +010063#define DEBUG_PARSE_NO_SPEEDUP
64#undef DEBUG_PARSE_NO_SPEEDUP
65
Willy Tarreau976f1ee2006-12-17 10:06:03 +010066/* This is used to perform a quick jump as an alternative to a break/continue
67 * instruction. The first argument is the label for normal operation, and the
68 * second one is the break/continue instruction in the no_speedup mode.
69 */
70
71#ifdef DEBUG_PARSE_NO_SPEEDUP
72#define QUICK_JUMP(x,y) y
73#else
74#define QUICK_JUMP(x,y) goto x
75#endif
76
Willy Tarreau1c47f852006-07-09 08:22:27 +020077/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010078const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020079 "HTTP/1.0 200 OK\r\n"
80 "Cache-Control: no-cache\r\n"
81 "Connection: close\r\n"
82 "Content-Type: text/html\r\n"
83 "\r\n"
84 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
85
Willy Tarreau0f772532006-12-23 20:51:41 +010086const struct chunk http_200_chunk = {
87 .str = (char *)&HTTP_200,
88 .len = sizeof(HTTP_200)-1
89};
90
91const char *HTTP_302 =
92 "HTTP/1.0 302 Found\r\n"
93 "Cache-Control: no-cache\r\n"
94 "Connection: close\r\n"
95 "Location: "; /* not terminated since it will be concatenated with the URL */
96
97/* same as 302 except that the browser MUST retry with the GET method */
98const char *HTTP_303 =
99 "HTTP/1.0 303 See Other\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Connection: close\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
105const char *HTTP_401_fmt =
106 "HTTP/1.0 401 Unauthorized\r\n"
107 "Cache-Control: no-cache\r\n"
108 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200109 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
111 "\r\n"
112 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
113
Willy Tarreau0f772532006-12-23 20:51:41 +0100114
115const int http_err_codes[HTTP_ERR_SIZE] = {
116 [HTTP_ERR_400] = 400,
117 [HTTP_ERR_403] = 403,
118 [HTTP_ERR_408] = 408,
119 [HTTP_ERR_500] = 500,
120 [HTTP_ERR_502] = 502,
121 [HTTP_ERR_503] = 503,
122 [HTTP_ERR_504] = 504,
123};
124
Willy Tarreau80587432006-12-24 17:47:20 +0100125static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100126 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100127 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100128 "Cache-Control: no-cache\r\n"
129 "Connection: close\r\n"
130 "Content-Type: text/html\r\n"
131 "\r\n"
132 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
133
134 [HTTP_ERR_403] =
135 "HTTP/1.0 403 Forbidden\r\n"
136 "Cache-Control: no-cache\r\n"
137 "Connection: close\r\n"
138 "Content-Type: text/html\r\n"
139 "\r\n"
140 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
141
142 [HTTP_ERR_408] =
143 "HTTP/1.0 408 Request Time-out\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
149
150 [HTTP_ERR_500] =
151 "HTTP/1.0 500 Server Error\r\n"
152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
157
158 [HTTP_ERR_502] =
159 "HTTP/1.0 502 Bad Gateway\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
165
166 [HTTP_ERR_503] =
167 "HTTP/1.0 503 Service Unavailable\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
173
174 [HTTP_ERR_504] =
175 "HTTP/1.0 504 Gateway Time-out\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
181
182};
183
Willy Tarreau80587432006-12-24 17:47:20 +0100184/* We must put the messages here since GCC cannot initialize consts depending
185 * on strlen().
186 */
187struct chunk http_err_chunks[HTTP_ERR_SIZE];
188
Willy Tarreau42250582007-04-01 01:30:43 +0200189#define FD_SETS_ARE_BITFIELDS
190#ifdef FD_SETS_ARE_BITFIELDS
191/*
192 * This map is used with all the FD_* macros to check whether a particular bit
193 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
194 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
195 * byte should be encoded. Be careful to always pass bytes from 0 to 255
196 * exclusively to the macros.
197 */
198fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
199fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
200
201#else
202#error "Check if your OS uses bitfields for fd_sets"
203#endif
204
Willy Tarreau80587432006-12-24 17:47:20 +0100205void init_proto_http()
206{
Willy Tarreau42250582007-04-01 01:30:43 +0200207 int i;
208 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100209 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200210
Willy Tarreau80587432006-12-24 17:47:20 +0100211 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
212 if (!http_err_msgs[msg]) {
213 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
214 abort();
215 }
216
217 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
218 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
219 }
Willy Tarreau42250582007-04-01 01:30:43 +0200220
221 /* initialize the log header encoding map : '{|}"#' should be encoded with
222 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
223 * URL encoding only requires '"', '#' to be encoded as well as non-
224 * printable characters above.
225 */
226 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
227 memset(url_encode_map, 0, sizeof(url_encode_map));
228 for (i = 0; i < 32; i++) {
229 FD_SET(i, hdr_encode_map);
230 FD_SET(i, url_encode_map);
231 }
232 for (i = 127; i < 256; i++) {
233 FD_SET(i, hdr_encode_map);
234 FD_SET(i, url_encode_map);
235 }
236
237 tmp = "\"#{|}";
238 while (*tmp) {
239 FD_SET(*tmp, hdr_encode_map);
240 tmp++;
241 }
242
243 tmp = "\"#";
244 while (*tmp) {
245 FD_SET(*tmp, url_encode_map);
246 tmp++;
247 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200248
249 /* memory allocations */
250 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200251 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100252}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200253
Willy Tarreau53b6c742006-12-17 13:37:46 +0100254/*
255 * We have 26 list of methods (1 per first letter), each of which can have
256 * up to 3 entries (2 valid, 1 null).
257 */
258struct http_method_desc {
259 http_meth_t meth;
260 int len;
261 const char text[8];
262};
263
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100264const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100265 ['C' - 'A'] = {
266 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
267 },
268 ['D' - 'A'] = {
269 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
270 },
271 ['G' - 'A'] = {
272 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
273 },
274 ['H' - 'A'] = {
275 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
276 },
277 ['P' - 'A'] = {
278 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
279 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
280 },
281 ['T' - 'A'] = {
282 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
283 },
284 /* rest is empty like this :
285 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
286 */
287};
288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100289/* It is about twice as fast on recent architectures to lookup a byte in a
290 * table than two perform a boolean AND or OR between two tests. Refer to
291 * RFC2616 for those chars.
292 */
293
294const char http_is_spht[256] = {
295 [' '] = 1, ['\t'] = 1,
296};
297
298const char http_is_crlf[256] = {
299 ['\r'] = 1, ['\n'] = 1,
300};
301
302const char http_is_lws[256] = {
303 [' '] = 1, ['\t'] = 1,
304 ['\r'] = 1, ['\n'] = 1,
305};
306
307const char http_is_sep[256] = {
308 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
309 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
310 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
311 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
312 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
313};
314
315const char http_is_ctl[256] = {
316 [0 ... 31] = 1,
317 [127] = 1,
318};
319
320/*
321 * A token is any ASCII char that is neither a separator nor a CTL char.
322 * Do not overwrite values in assignment since gcc-2.95 will not handle
323 * them correctly. Instead, define every non-CTL char's status.
324 */
325const char http_is_token[256] = {
326 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
327 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
328 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
329 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
330 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
331 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
332 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
333 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
334 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
335 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
336 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
337 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
338 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
339 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
340 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
341 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
342 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
343 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
344 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
345 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
346 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
347 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
348 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
349 ['|'] = 1, ['}'] = 0, ['~'] = 1,
350};
351
352
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100353/*
354 * An http ver_token is any ASCII which can be found in an HTTP version,
355 * which includes 'H', 'T', 'P', '/', '.' and any digit.
356 */
357const char http_is_ver_token[256] = {
358 ['.'] = 1, ['/'] = 1,
359 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
360 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
361 ['H'] = 1, ['P'] = 1, ['T'] = 1,
362};
363
364
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365#ifdef DEBUG_FULL
366static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" };
367static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" };
368#endif
369
Willy Tarreau42250582007-04-01 01:30:43 +0200370static void http_sess_log(struct session *s);
371
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100372/*
373 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
374 * CRLF. Text length is measured first, so it cannot be NULL.
375 * The header is also automatically added to the index <hdr_idx>, and the end
376 * of headers is automatically adjusted. The number of bytes added is returned
377 * on success, otherwise <0 is returned indicating an error.
378 */
379int http_header_add_tail(struct buffer *b, struct http_msg *msg,
380 struct hdr_idx *hdr_idx, const char *text)
381{
382 int bytes, len;
383
384 len = strlen(text);
385 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
386 if (!bytes)
387 return -1;
388 msg->eoh += bytes;
389 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
390}
391
392/*
393 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
394 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
395 * the buffer is only opened and the space reserved, but nothing is copied.
396 * The header is also automatically added to the index <hdr_idx>, and the end
397 * of headers is automatically adjusted. The number of bytes added is returned
398 * on success, otherwise <0 is returned indicating an error.
399 */
400int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
401 struct hdr_idx *hdr_idx, const char *text, int len)
402{
403 int bytes;
404
405 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
406 if (!bytes)
407 return -1;
408 msg->eoh += bytes;
409 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
410}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411
412/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100413 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
414 * If so, returns the position of the first non-space character relative to
415 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
416 * to return a pointer to the place after the first space. Returns 0 if the
417 * header name does not match. Checks are case-insensitive.
418 */
419int http_header_match2(const char *hdr, const char *end,
420 const char *name, int len)
421{
422 const char *val;
423
424 if (hdr + len >= end)
425 return 0;
426 if (hdr[len] != ':')
427 return 0;
428 if (strncasecmp(hdr, name, len) != 0)
429 return 0;
430 val = hdr + len + 1;
431 while (val < end && HTTP_IS_SPHT(*val))
432 val++;
433 if ((val >= end) && (len + 2 <= end - hdr))
434 return len + 2; /* we may replace starting from second space */
435 return val - hdr;
436}
437
Willy Tarreau33a7e692007-06-10 19:45:56 +0200438/* Find the end of the header value contained between <s> and <e>.
439 * See RFC2616, par 2.2 for more information. Note that it requires
440 * a valid header to return a valid result.
441 */
442const char *find_hdr_value_end(const char *s, const char *e)
443{
444 int quoted, qdpair;
445
446 quoted = qdpair = 0;
447 for (; s < e; s++) {
448 if (qdpair) qdpair = 0;
449 else if (quoted && *s == '\\') qdpair = 1;
450 else if (quoted && *s == '"') quoted = 0;
451 else if (*s == '"') quoted = 1;
452 else if (*s == ',') return s;
453 }
454 return s;
455}
456
457/* Find the first or next occurrence of header <name> in message buffer <sol>
458 * using headers index <idx>, and return it in the <ctx> structure. This
459 * structure holds everything necessary to use the header and find next
460 * occurrence. If its <idx> member is 0, the header is searched from the
461 * beginning. Otherwise, the next occurrence is returned. The function returns
462 * 1 when it finds a value, and 0 when there is no more.
463 */
464int http_find_header2(const char *name, int len,
465 const char *sol, struct hdr_idx *idx,
466 struct hdr_ctx *ctx)
467{
468 __label__ return_hdr, next_hdr;
469 const char *eol, *sov;
470 int cur_idx;
471
472 if (ctx->idx) {
473 /* We have previously returned a value, let's search
474 * another one on the same line.
475 */
476 cur_idx = ctx->idx;
477 sol = ctx->line;
478 sov = sol + ctx->val + ctx->vlen;
479 eol = sol + idx->v[cur_idx].len;
480
481 if (sov >= eol)
482 /* no more values in this header */
483 goto next_hdr;
484
485 /* values remaining for this header, skip the comma */
486 sov++;
487 while (sov < eol && http_is_lws[(unsigned char)*sov])
488 sov++;
489
490 goto return_hdr;
491 }
492
493 /* first request for this header */
494 sol += hdr_idx_first_pos(idx);
495 cur_idx = hdr_idx_first_idx(idx);
496
497 while (cur_idx) {
498 eol = sol + idx->v[cur_idx].len;
499
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200500 if (len == 0) {
501 /* No argument was passed, we want any header.
502 * To achieve this, we simply build a fake request. */
503 while (sol + len < eol && sol[len] != ':')
504 len++;
505 name = sol;
506 }
507
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 if ((len < eol - sol) &&
509 (sol[len] == ':') &&
510 (strncasecmp(sol, name, len) == 0)) {
511
512 sov = sol + len + 1;
513 while (sov < eol && http_is_lws[(unsigned char)*sov])
514 sov++;
515 return_hdr:
516 ctx->line = sol;
517 ctx->idx = cur_idx;
518 ctx->val = sov - sol;
519
520 eol = find_hdr_value_end(sov, eol);
521 ctx->vlen = eol - sov;
522 return 1;
523 }
524 next_hdr:
525 sol = eol + idx->v[cur_idx].cr + 1;
526 cur_idx = idx->v[cur_idx].next;
527 }
528 return 0;
529}
530
531int http_find_header(const char *name,
532 const char *sol, struct hdr_idx *idx,
533 struct hdr_ctx *ctx)
534{
535 return http_find_header2(name, strlen(name), sol, idx, ctx);
536}
537
Willy Tarreaubaaee002006-06-26 02:48:02 +0200538/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100539 * indicators accordingly. Note that if <status> is 0, or if the message
540 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200541 */
542void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100543 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544{
545 t->srv_state = SV_STCLOSE;
Willy Tarreau0f772532006-12-23 20:51:41 +0100546 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100547 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100548 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100549 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200550 }
551 if (!(t->flags & SN_ERR_MASK))
552 t->flags |= err;
553 if (!(t->flags & SN_FINST_MASK))
554 t->flags |= finst;
555}
556
Willy Tarreau80587432006-12-24 17:47:20 +0100557/* This function returns the appropriate error location for the given session
558 * and message.
559 */
560
561struct chunk *error_message(struct session *s, int msgnum)
562{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200563 if (s->be->errmsg[msgnum].str)
564 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100565 else if (s->fe->errmsg[msgnum].str)
566 return &s->fe->errmsg[msgnum];
567 else
568 return &http_err_chunks[msgnum];
569}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570
Willy Tarreau53b6c742006-12-17 13:37:46 +0100571/*
572 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
573 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
574 */
575static http_meth_t find_http_meth(const char *str, const int len)
576{
577 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100578 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100579
580 m = ((unsigned)*str - 'A');
581
582 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100583 for (h = http_methods[m]; h->len > 0; h++) {
584 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100585 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100586 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100587 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100588 };
589 return HTTP_METH_OTHER;
590 }
591 return HTTP_METH_NONE;
592
593}
594
Willy Tarreaubaaee002006-06-26 02:48:02 +0200595/* Processes the client and server jobs of a session task, then
596 * puts it back to the wait queue in a clean state, or
597 * cleans up its resources if it must be deleted. Returns
598 * the time the task accepts to wait, or TIME_ETERNITY for
599 * infinity.
600 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200601void process_session(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200602{
603 struct session *s = t->context;
604 int fsm_resync = 0;
605
606 do {
607 fsm_resync = 0;
608 //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
609 fsm_resync |= process_cli(s);
610 //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
611 fsm_resync |= process_srv(s);
612 //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
613 } while (fsm_resync);
614
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200615 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200616 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
617 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200618
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200619 t->expire = s->req->rex;
620 tv_min(&t->expire, &s->req->rex, &s->req->wex);
621 tv_bound(&t->expire, &s->req->cex);
622 tv_bound(&t->expire, &s->rep->rex);
623 tv_bound(&t->expire, &s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200624
625 /* restore t to its place in the task list */
626 task_queue(t);
627
628#ifdef DEBUG_FULL
629 /* DEBUG code : this should never ever happen, otherwise it indicates
630 * that a task still has something to do and will provoke a quick loop.
631 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200632 if (tv_remain2(&now, &t->expire) <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200633 exit(100);
634#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200635 *next = t->expire;
636 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637 }
638
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100639 s->fe->feconn--;
640 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200641 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200642 actconn--;
643
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200644 if (unlikely((global.mode & MODE_DEBUG) &&
645 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200646 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100647 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200648 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100649 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200650 write(1, trash, len);
651 }
652
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200653 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau35d66b02007-01-02 00:28:21 +0100654 if (s->req != NULL)
655 s->logs.bytes_in = s->req->total;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200656 if (s->rep != NULL)
Willy Tarreau35d66b02007-01-02 00:28:21 +0100657 s->logs.bytes_out = s->rep->total;
658
659 s->fe->bytes_in += s->logs.bytes_in;
660 s->fe->bytes_out += s->logs.bytes_out;
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200661 if (s->be != s->fe) {
662 s->be->bytes_in += s->logs.bytes_in;
663 s->be->bytes_out += s->logs.bytes_out;
Willy Tarreau35d66b02007-01-02 00:28:21 +0100664 }
665 if (s->srv) {
666 s->srv->bytes_in += s->logs.bytes_in;
667 s->srv->bytes_out += s->logs.bytes_out;
668 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669
670 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200671 if (s->logs.logwait &&
672 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200673 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
674 if (s->fe->to_log & LW_REQ)
675 http_sess_log(s);
676 else
677 tcp_sess_log(s);
678 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679
680 /* the task MUST not be in the run queue anymore */
681 task_delete(t);
682 session_free(s);
683 task_free(t);
Willy Tarreaud825eef2007-05-12 22:35:00 +0200684 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200685}
686
687
Willy Tarreau42250582007-04-01 01:30:43 +0200688extern const char sess_term_cond[8];
689extern const char sess_fin_state[8];
690extern const char *monthname[12];
691const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
692const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
693 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
694 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200695struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200696struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100697
Willy Tarreau42250582007-04-01 01:30:43 +0200698/*
699 * send a log for the session when we have enough info about it.
700 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100701 */
Willy Tarreau42250582007-04-01 01:30:43 +0200702static void http_sess_log(struct session *s)
703{
704 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
705 struct proxy *fe = s->fe;
706 struct proxy *be = s->be;
707 struct proxy *prx_log;
708 struct http_txn *txn = &s->txn;
709 int tolog;
710 char *uri, *h;
711 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200712 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200713 static char tmpline[MAX_SYSLOG_LEN];
714 int hdr;
715
716 if (fe->logfac1 < 0 && fe->logfac2 < 0)
717 return;
718 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100719
Willy Tarreau42250582007-04-01 01:30:43 +0200720 if (s->cli_addr.ss_family == AF_INET)
721 inet_ntop(AF_INET,
722 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
723 pn, sizeof(pn));
724 else
725 inet_ntop(AF_INET6,
726 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
727 pn, sizeof(pn));
728
Willy Tarreaufe944602007-10-25 10:34:16 +0200729 get_localtime(s->logs.tv_accept.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200730
731 /* FIXME: let's limit ourselves to frontend logging for now. */
732 tolog = fe->to_log;
733
734 h = tmpline;
735 if (fe->to_log & LW_REQHDR &&
736 txn->req.cap &&
737 (h < tmpline + sizeof(tmpline) - 10)) {
738 *(h++) = ' ';
739 *(h++) = '{';
740 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
741 if (hdr)
742 *(h++) = '|';
743 if (txn->req.cap[hdr] != NULL)
744 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
745 '#', hdr_encode_map, txn->req.cap[hdr]);
746 }
747 *(h++) = '}';
748 }
749
750 if (fe->to_log & LW_RSPHDR &&
751 txn->rsp.cap &&
752 (h < tmpline + sizeof(tmpline) - 7)) {
753 *(h++) = ' ';
754 *(h++) = '{';
755 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
756 if (hdr)
757 *(h++) = '|';
758 if (txn->rsp.cap[hdr] != NULL)
759 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
760 '#', hdr_encode_map, txn->rsp.cap[hdr]);
761 }
762 *(h++) = '}';
763 }
764
765 if (h < tmpline + sizeof(tmpline) - 4) {
766 *(h++) = ' ';
767 *(h++) = '"';
768 uri = txn->uri ? txn->uri : "<BADREQ>";
769 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
770 '#', url_encode_map, uri);
771 *(h++) = '"';
772 }
773 *h = '\0';
774
775 svid = (tolog & LW_SVID) ?
776 (s->data_source != DATA_SRC_STATS) ?
777 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
778
779 send_log(prx_log, LOG_INFO,
780 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
781 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
782 " %s %s %c%c%c%c %d/%d/%d/%d %d/%d%s\n",
783 pn,
784 (s->cli_addr.ss_family == AF_INET) ?
785 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
786 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200787 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
788 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.tv_accept.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200789 fe->id, be->id, svid,
790 s->logs.t_request,
791 (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1,
792 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
793 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
794 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
795 txn->status,
796 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in,
797 txn->cli_cookie ? txn->cli_cookie : "-",
798 txn->srv_cookie ? txn->srv_cookie : "-",
799 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
800 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
801 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
802 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
803 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
804 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
805
806 s->logs.logwait = 0;
807}
808
Willy Tarreau117f59e2007-03-04 18:17:17 +0100809
810/*
811 * Capture headers from message starting at <som> according to header list
812 * <cap_hdr>, and fill the <idx> structure appropriately.
813 */
814void capture_headers(char *som, struct hdr_idx *idx,
815 char **cap, struct cap_hdr *cap_hdr)
816{
817 char *eol, *sol, *col, *sov;
818 int cur_idx;
819 struct cap_hdr *h;
820 int len;
821
822 sol = som + hdr_idx_first_pos(idx);
823 cur_idx = hdr_idx_first_idx(idx);
824
825 while (cur_idx) {
826 eol = sol + idx->v[cur_idx].len;
827
828 col = sol;
829 while (col < eol && *col != ':')
830 col++;
831
832 sov = col + 1;
833 while (sov < eol && http_is_lws[(unsigned char)*sov])
834 sov++;
835
836 for (h = cap_hdr; h; h = h->next) {
837 if ((h->namelen == col - sol) &&
838 (strncasecmp(sol, h->name, h->namelen) == 0)) {
839 if (cap[h->index] == NULL)
840 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200841 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100842
843 if (cap[h->index] == NULL) {
844 Alert("HTTP capture : out of memory.\n");
845 continue;
846 }
847
848 len = eol - sov;
849 if (len > h->len)
850 len = h->len;
851
852 memcpy(cap[h->index], sov, len);
853 cap[h->index][len]=0;
854 }
855 }
856 sol = eol + idx->v[cur_idx].cr + 1;
857 cur_idx = idx->v[cur_idx].next;
858 }
859}
860
861
Willy Tarreau42250582007-04-01 01:30:43 +0200862/* either we find an LF at <ptr> or we jump to <bad>.
863 */
864#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
865
866/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
867 * otherwise to <http_msg_ood> with <state> set to <st>.
868 */
869#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
870 ptr++; \
871 if (likely(ptr < end)) \
872 goto good; \
873 else { \
874 state = (st); \
875 goto http_msg_ood; \
876 } \
877 } while (0)
878
879
Willy Tarreaubaaee002006-06-26 02:48:02 +0200880/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100881 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100882 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
883 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
884 * will give undefined results.
885 * Note that it is upon the caller's responsibility to ensure that ptr < end,
886 * and that msg->sol points to the beginning of the response.
887 * If a complete line is found (which implies that at least one CR or LF is
888 * found before <end>, the updated <ptr> is returned, otherwise NULL is
889 * returned indicating an incomplete line (which does not mean that parts have
890 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
891 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
892 * upon next call.
893 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200894 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100895 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
896 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200897 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100898 */
Willy Tarreaua15645d2007-03-18 16:22:39 +0100899const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100900 const char *ptr, const char *end,
901 char **ret_ptr, int *ret_state)
902{
903 __label__
904 http_msg_rpver,
905 http_msg_rpver_sp,
906 http_msg_rpcode,
907 http_msg_rpcode_sp,
908 http_msg_rpreason,
909 http_msg_rpline_eol,
910 http_msg_ood, /* out of data */
911 http_msg_invalid;
912
913 switch (state) {
914 http_msg_rpver:
915 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100916 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100917 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
918
919 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100920 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100921 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
922 }
923 goto http_msg_invalid;
924
925 http_msg_rpver_sp:
926 case HTTP_MSG_RPVER_SP:
927 if (likely(!HTTP_IS_LWS(*ptr))) {
928 msg->sl.st.c = ptr - msg_buf;
929 goto http_msg_rpcode;
930 }
931 if (likely(HTTP_IS_SPHT(*ptr)))
932 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
933 /* so it's a CR/LF, this is invalid */
934 goto http_msg_invalid;
935
936 http_msg_rpcode:
937 case HTTP_MSG_RPCODE:
938 if (likely(!HTTP_IS_LWS(*ptr)))
939 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
940
941 if (likely(HTTP_IS_SPHT(*ptr))) {
942 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
943 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
944 }
945
946 /* so it's a CR/LF, so there is no reason phrase */
947 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
948 http_msg_rsp_reason:
949 /* FIXME: should we support HTTP responses without any reason phrase ? */
950 msg->sl.st.r = ptr - msg_buf;
951 msg->sl.st.r_l = 0;
952 goto http_msg_rpline_eol;
953
954 http_msg_rpcode_sp:
955 case HTTP_MSG_RPCODE_SP:
956 if (likely(!HTTP_IS_LWS(*ptr))) {
957 msg->sl.st.r = ptr - msg_buf;
958 goto http_msg_rpreason;
959 }
960 if (likely(HTTP_IS_SPHT(*ptr)))
961 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
962 /* so it's a CR/LF, so there is no reason phrase */
963 goto http_msg_rsp_reason;
964
965 http_msg_rpreason:
966 case HTTP_MSG_RPREASON:
967 if (likely(!HTTP_IS_CRLF(*ptr)))
968 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
969 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
970 http_msg_rpline_eol:
971 /* We have seen the end of line. Note that we do not
972 * necessarily have the \n yet, but at least we know that we
973 * have EITHER \r OR \n, otherwise the response would not be
974 * complete. We can then record the response length and return
975 * to the caller which will be able to register it.
976 */
977 msg->sl.st.l = ptr - msg->sol;
978 return ptr;
979
980#ifdef DEBUG_FULL
981 default:
982 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
983 exit(1);
984#endif
985 }
986
987 http_msg_ood:
988 /* out of data */
989 if (ret_state)
990 *ret_state = state;
991 if (ret_ptr)
992 *ret_ptr = (char *)ptr;
993 return NULL;
994
995 http_msg_invalid:
996 /* invalid message */
997 if (ret_state)
998 *ret_state = HTTP_MSG_ERROR;
999 return NULL;
1000}
1001
1002
1003/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001004 * This function parses a request line between <ptr> and <end>, starting with
1005 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1006 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1007 * will give undefined results.
1008 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1009 * and that msg->sol points to the beginning of the request.
1010 * If a complete line is found (which implies that at least one CR or LF is
1011 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1012 * returned indicating an incomplete line (which does not mean that parts have
1013 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1014 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1015 * upon next call.
1016 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001017 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001018 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1019 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001020 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001021 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001022const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +01001023 const char *ptr, const char *end,
1024 char **ret_ptr, int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001025{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001026 __label__
1027 http_msg_rqmeth,
1028 http_msg_rqmeth_sp,
1029 http_msg_rquri,
1030 http_msg_rquri_sp,
1031 http_msg_rqver,
1032 http_msg_rqline_eol,
1033 http_msg_ood, /* out of data */
1034 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001035
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001036 switch (state) {
1037 http_msg_rqmeth:
1038 case HTTP_MSG_RQMETH:
1039 if (likely(HTTP_IS_TOKEN(*ptr)))
1040 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001041
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001042 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001043 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001044 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1045 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001046
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001047 if (likely(HTTP_IS_CRLF(*ptr))) {
1048 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001049 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001050 http_msg_req09_uri:
1051 msg->sl.rq.u = ptr - msg_buf;
1052 http_msg_req09_uri_e:
1053 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1054 http_msg_req09_ver:
1055 msg->sl.rq.v = ptr - msg_buf;
1056 msg->sl.rq.v_l = 0;
1057 goto http_msg_rqline_eol;
1058 }
1059 goto http_msg_invalid;
1060
1061 http_msg_rqmeth_sp:
1062 case HTTP_MSG_RQMETH_SP:
1063 if (likely(!HTTP_IS_LWS(*ptr))) {
1064 msg->sl.rq.u = ptr - msg_buf;
1065 goto http_msg_rquri;
1066 }
1067 if (likely(HTTP_IS_SPHT(*ptr)))
1068 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1069 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1070 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001071
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001072 http_msg_rquri:
1073 case HTTP_MSG_RQURI:
1074 if (likely(!HTTP_IS_LWS(*ptr)))
1075 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 if (likely(HTTP_IS_SPHT(*ptr))) {
1078 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1079 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1080 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001081
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001082 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1083 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001084
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001085 http_msg_rquri_sp:
1086 case HTTP_MSG_RQURI_SP:
1087 if (likely(!HTTP_IS_LWS(*ptr))) {
1088 msg->sl.rq.v = ptr - msg_buf;
1089 goto http_msg_rqver;
1090 }
1091 if (likely(HTTP_IS_SPHT(*ptr)))
1092 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1093 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1094 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001095
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001096 http_msg_rqver:
1097 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001098 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001099 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001100
1101 if (likely(HTTP_IS_CRLF(*ptr))) {
1102 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1103 http_msg_rqline_eol:
1104 /* We have seen the end of line. Note that we do not
1105 * necessarily have the \n yet, but at least we know that we
1106 * have EITHER \r OR \n, otherwise the request would not be
1107 * complete. We can then record the request length and return
1108 * to the caller which will be able to register it.
1109 */
1110 msg->sl.rq.l = ptr - msg->sol;
1111 return ptr;
1112 }
1113
1114 /* neither an HTTP_VER token nor a CRLF */
1115 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001116
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001117#ifdef DEBUG_FULL
1118 default:
1119 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1120 exit(1);
1121#endif
1122 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001123
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001124 http_msg_ood:
1125 /* out of data */
1126 if (ret_state)
1127 *ret_state = state;
1128 if (ret_ptr)
1129 *ret_ptr = (char *)ptr;
1130 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001132 http_msg_invalid:
1133 /* invalid message */
1134 if (ret_state)
1135 *ret_state = HTTP_MSG_ERROR;
1136 return NULL;
1137}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001138
1139
Willy Tarreau8973c702007-01-21 23:58:29 +01001140/*
1141 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001142 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001143 * when data are missing and recalled at the exact same location with no
1144 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001145 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1146 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001147 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001148void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1149{
1150 __label__
1151 http_msg_rqbefore,
1152 http_msg_rqbefore_cr,
1153 http_msg_rqmeth,
1154 http_msg_rqline_end,
1155 http_msg_hdr_first,
1156 http_msg_hdr_name,
1157 http_msg_hdr_l1_sp,
1158 http_msg_hdr_l1_lf,
1159 http_msg_hdr_l1_lws,
1160 http_msg_hdr_val,
1161 http_msg_hdr_l2_lf,
1162 http_msg_hdr_l2_lws,
1163 http_msg_complete_header,
1164 http_msg_last_lf,
1165 http_msg_ood, /* out of data */
1166 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001167
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168 int state; /* updated only when leaving the FSM */
1169 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001170
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001171 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001172 ptr = buf->lr;
1173 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001174
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001175 if (unlikely(ptr >= end))
1176 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001177
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001178 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 /*
1180 * First, states that are specific to the response only.
1181 * We check them first so that request and headers are
1182 * closer to each other (accessed more often).
1183 */
1184 http_msg_rpbefore:
1185 case HTTP_MSG_RPBEFORE:
1186 if (likely(HTTP_IS_TOKEN(*ptr))) {
1187 if (likely(ptr == buf->data)) {
1188 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001189 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 } else {
1191#if PARSE_PRESERVE_EMPTY_LINES
1192 /* only skip empty leading lines, don't remove them */
1193 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001194 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001195#else
1196 /* Remove empty leading lines, as recommended by
1197 * RFC2616. This takes a lot of time because we
1198 * must move all the buffer backwards, but this
1199 * is rarely needed. The method above will be
1200 * cleaner when we'll be able to start sending
1201 * the request from any place in the buffer.
1202 */
1203 buf->lr = ptr;
1204 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001205 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001206 msg->sol = buf->data;
1207 ptr = buf->data;
1208 end = buf->r;
1209#endif
1210 }
1211 hdr_idx_init(idx);
1212 state = HTTP_MSG_RPVER;
1213 goto http_msg_rpver;
1214 }
1215
1216 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1217 goto http_msg_invalid;
1218
1219 if (unlikely(*ptr == '\n'))
1220 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1221 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1222 /* stop here */
1223
1224 http_msg_rpbefore_cr:
1225 case HTTP_MSG_RPBEFORE_CR:
1226 EXPECT_LF_HERE(ptr, http_msg_invalid);
1227 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1228 /* stop here */
1229
1230 http_msg_rpver:
1231 case HTTP_MSG_RPVER:
1232 case HTTP_MSG_RPVER_SP:
1233 case HTTP_MSG_RPCODE:
1234 case HTTP_MSG_RPCODE_SP:
1235 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001236 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001237 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001238 if (unlikely(!ptr))
1239 return;
1240
1241 /* we have a full response and we know that we have either a CR
1242 * or an LF at <ptr>.
1243 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001244 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001245 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1246
1247 msg->sol = ptr;
1248 if (likely(*ptr == '\r'))
1249 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1250 goto http_msg_rpline_end;
1251
1252 http_msg_rpline_end:
1253 case HTTP_MSG_RPLINE_END:
1254 /* msg->sol must point to the first of CR or LF. */
1255 EXPECT_LF_HERE(ptr, http_msg_invalid);
1256 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1257 /* stop here */
1258
1259 /*
1260 * Second, states that are specific to the request only
1261 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001262 http_msg_rqbefore:
1263 case HTTP_MSG_RQBEFORE:
1264 if (likely(HTTP_IS_TOKEN(*ptr))) {
1265 if (likely(ptr == buf->data)) {
1266 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001267 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001268 } else {
1269#if PARSE_PRESERVE_EMPTY_LINES
1270 /* only skip empty leading lines, don't remove them */
1271 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001272 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001273#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001274 /* Remove empty leading lines, as recommended by
1275 * RFC2616. This takes a lot of time because we
1276 * must move all the buffer backwards, but this
1277 * is rarely needed. The method above will be
1278 * cleaner when we'll be able to start sending
1279 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001280 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001281 buf->lr = ptr;
1282 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001283 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 msg->sol = buf->data;
1285 ptr = buf->data;
1286 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001287#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001288 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001289 /* we will need this when keep-alive will be supported
1290 hdr_idx_init(idx);
1291 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001292 state = HTTP_MSG_RQMETH;
1293 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001294 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001295
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001296 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1297 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001299 if (unlikely(*ptr == '\n'))
1300 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1301 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001302 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001303
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001304 http_msg_rqbefore_cr:
1305 case HTTP_MSG_RQBEFORE_CR:
1306 EXPECT_LF_HERE(ptr, http_msg_invalid);
1307 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001309
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 http_msg_rqmeth:
1311 case HTTP_MSG_RQMETH:
1312 case HTTP_MSG_RQMETH_SP:
1313 case HTTP_MSG_RQURI:
1314 case HTTP_MSG_RQURI_SP:
1315 case HTTP_MSG_RQVER:
1316 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001317 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 if (unlikely(!ptr))
1319 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001320
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 /* we have a full request and we know that we have either a CR
1322 * or an LF at <ptr>.
1323 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001324 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001326
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001327 msg->sol = ptr;
1328 if (likely(*ptr == '\r'))
1329 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001331
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001332 http_msg_rqline_end:
1333 case HTTP_MSG_RQLINE_END:
1334 /* check for HTTP/0.9 request : no version information available.
1335 * msg->sol must point to the first of CR or LF.
1336 */
1337 if (unlikely(msg->sl.rq.v_l == 0))
1338 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001339
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 EXPECT_LF_HERE(ptr, http_msg_invalid);
1341 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001342 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001343
Willy Tarreau8973c702007-01-21 23:58:29 +01001344 /*
1345 * Common states below
1346 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 http_msg_hdr_first:
1348 case HTTP_MSG_HDR_FIRST:
1349 msg->sol = ptr;
1350 if (likely(!HTTP_IS_CRLF(*ptr))) {
1351 goto http_msg_hdr_name;
1352 }
1353
1354 if (likely(*ptr == '\r'))
1355 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1356 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001357
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001358 http_msg_hdr_name:
1359 case HTTP_MSG_HDR_NAME:
1360 /* assumes msg->sol points to the first char */
1361 if (likely(HTTP_IS_TOKEN(*ptr)))
1362 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001363
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 if (likely(*ptr == ':')) {
1365 msg->col = ptr - buf->data;
1366 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1367 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001368
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001369 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001370
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 http_msg_hdr_l1_sp:
1372 case HTTP_MSG_HDR_L1_SP:
1373 /* assumes msg->sol points to the first char and msg->col to the colon */
1374 if (likely(HTTP_IS_SPHT(*ptr)))
1375 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001376
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001377 /* header value can be basically anything except CR/LF */
1378 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001380 if (likely(!HTTP_IS_CRLF(*ptr))) {
1381 goto http_msg_hdr_val;
1382 }
1383
1384 if (likely(*ptr == '\r'))
1385 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1386 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 http_msg_hdr_l1_lf:
1389 case HTTP_MSG_HDR_L1_LF:
1390 EXPECT_LF_HERE(ptr, http_msg_invalid);
1391 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 http_msg_hdr_l1_lws:
1394 case HTTP_MSG_HDR_L1_LWS:
1395 if (likely(HTTP_IS_SPHT(*ptr))) {
1396 /* replace HT,CR,LF with spaces */
1397 for (; buf->data+msg->sov < ptr; msg->sov++)
1398 buf->data[msg->sov] = ' ';
1399 goto http_msg_hdr_l1_sp;
1400 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001401 /* we had a header consisting only in spaces ! */
1402 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 goto http_msg_complete_header;
1404
1405 http_msg_hdr_val:
1406 case HTTP_MSG_HDR_VAL:
1407 /* assumes msg->sol points to the first char, msg->col to the
1408 * colon, and msg->sov points to the first character of the
1409 * value.
1410 */
1411 if (likely(!HTTP_IS_CRLF(*ptr)))
1412 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001413
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 msg->eol = ptr;
1415 /* Note: we could also copy eol into ->eoh so that we have the
1416 * real header end in case it ends with lots of LWS, but is this
1417 * really needed ?
1418 */
1419 if (likely(*ptr == '\r'))
1420 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1421 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 http_msg_hdr_l2_lf:
1424 case HTTP_MSG_HDR_L2_LF:
1425 EXPECT_LF_HERE(ptr, http_msg_invalid);
1426 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001427
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 http_msg_hdr_l2_lws:
1429 case HTTP_MSG_HDR_L2_LWS:
1430 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1431 /* LWS: replace HT,CR,LF with spaces */
1432 for (; msg->eol < ptr; msg->eol++)
1433 *msg->eol = ' ';
1434 goto http_msg_hdr_val;
1435 }
1436 http_msg_complete_header:
1437 /*
1438 * It was a new header, so the last one is finished.
1439 * Assumes msg->sol points to the first char, msg->col to the
1440 * colon, msg->sov points to the first character of the value
1441 * and msg->eol to the first CR or LF so we know how the line
1442 * ends. We insert last header into the index.
1443 */
1444 /*
1445 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1446 write(2, msg->sol, msg->eol-msg->sol);
1447 fprintf(stderr,"\n");
1448 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1451 idx, idx->tail) < 0))
1452 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001453
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 msg->sol = ptr;
1455 if (likely(!HTTP_IS_CRLF(*ptr))) {
1456 goto http_msg_hdr_name;
1457 }
1458
1459 if (likely(*ptr == '\r'))
1460 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1461 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001462
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 http_msg_last_lf:
1464 case HTTP_MSG_LAST_LF:
1465 /* Assumes msg->sol points to the first of either CR or LF */
1466 EXPECT_LF_HERE(ptr, http_msg_invalid);
1467 ptr++;
1468 buf->lr = ptr;
1469 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001470 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 return;
1472#ifdef DEBUG_FULL
1473 default:
1474 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1475 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001476#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 }
1478 http_msg_ood:
1479 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001480 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 buf->lr = ptr;
1482 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 http_msg_invalid:
1485 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001486 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 return;
1488}
1489
1490/*
1491 * manages the client FSM and its socket. BTW, it also tries to handle the
1492 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1493 * 0 else.
1494 */
1495int process_cli(struct session *t)
1496{
1497 int s = t->srv_state;
1498 int c = t->cli_state;
1499 struct buffer *req = t->req;
1500 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001501
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
1503 cli_stnames[c], srv_stnames[s],
Willy Tarreauf161a342007-04-08 16:59:42 +02001504 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 req->rex.tv_sec, req->rex.tv_usec,
1506 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001507
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 if (c == CL_STHEADERS) {
1509 /*
1510 * Now parse the partial (or complete) lines.
1511 * We will check the request syntax, and also join multi-line
1512 * headers. An index of all the lines will be elaborated while
1513 * parsing.
1514 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001515 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001518 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 * req->data + req->eoh = end of processed headers / start of current one
1520 * req->data + req->eol = end of current header or line (LF or CRLF)
1521 * req->lr = first non-visited byte
1522 * req->r = end of data
1523 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001526 struct http_txn *txn = &t->txn;
1527 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001529
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001531 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001532
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 /* 1: we might have to print this header in debug mode */
1534 if (unlikely((global.mode & MODE_DEBUG) &&
1535 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001536 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001538
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001539 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 eol = sol + msg->sl.rq.l;
1541 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001542
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001543 sol += hdr_idx_first_pos(&txn->hdr_idx);
1544 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001545
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001547 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001549 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1550 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 }
1552 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001553
Willy Tarreau58f10d72006-12-04 02:26:12 +01001554
1555 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001557 * If not so, we check the FD and buffer states before leaving.
1558 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1560 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001561 *
1562 */
1563
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001564 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 /*
1566 * First, let's catch bad requests.
1567 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001568 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001570
1571 /* 1: Since we are in header mode, if there's no space
1572 * left for headers, we won't be able to free more
1573 * later, so the session will never terminate. We
1574 * must terminate it now.
1575 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001576 if (unlikely(req->l >= req->rlim - req->data)) {
1577 /* FIXME: check if URI is set and return Status
1578 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001579 */
Willy Tarreau06619262006-12-17 08:37:22 +01001580 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001581 }
1582
1583 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1585 /* read error, or last read : give up. */
Willy Tarreaufa645582007-06-03 15:59:52 +02001586 buffer_shutr(req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001587 fd_delete(t->cli_fd);
1588 t->cli_state = CL_STCLOSE;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001589 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001590 if (!(t->flags & SN_ERR_MASK))
1591 t->flags |= SN_ERR_CLICL;
1592 if (!(t->flags & SN_FINST_MASK))
1593 t->flags |= SN_FINST_R;
1594 return 1;
1595 }
1596
1597 /* 3: has the read timeout expired ? */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001598 else if (unlikely(tv_isle(&req->rex, &now))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001599 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001600 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001601 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001602 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001603 if (!(t->flags & SN_ERR_MASK))
1604 t->flags |= SN_ERR_CLITO;
1605 if (!(t->flags & SN_FINST_MASK))
1606 t->flags |= SN_FINST_R;
1607 return 1;
1608 }
1609
1610 /* 4: do we need to re-enable the read socket ? */
Willy Tarreau66319382007-04-08 17:17:37 +02001611 else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001612 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreau58f10d72006-12-04 02:26:12 +01001613 * full. We cannot loop here since stream_sock_read will disable it only if
1614 * req->l == rlim-data
1615 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001616 if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout))
Willy Tarreau58f10d72006-12-04 02:26:12 +01001617 tv_eternity(&req->rex);
1618 }
1619 return t->cli_state != CL_STHEADERS;
1620 }
1621
1622
1623 /****************************************************************
1624 * More interesting part now : we know that we have a complete *
1625 * request which at least looks like HTTP. We have an indicator *
1626 * of each header's length, so we can parse them quickly. *
1627 ****************************************************************/
1628
Willy Tarreau9cdde232007-05-02 20:58:19 +02001629 /* ensure we keep this pointer to the beginning of the message */
1630 msg->sol = req->data + msg->som;
1631
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 /*
1633 * 1: identify the method
1634 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001635 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001636
1637 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001639 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001641 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 if (unlikely((t->fe->monitor_uri_len != 0) &&
1643 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1644 !memcmp(&req->data[msg->sl.rq.u],
1645 t->fe->monitor_uri,
1646 t->fe->monitor_uri_len))) {
1647 /*
1648 * We have found the monitor URI
1649 */
1650 t->flags |= SN_MONITOR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001651 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001652 client_retnclose(t, &http_200_chunk);
1653 goto return_prx_cond;
1654 }
1655
1656 /*
1657 * 3: Maybe we have to copy the original REQURI for the logs ?
1658 * Note: we cannot log anymore if the request has been
1659 * classified as invalid.
1660 */
1661 if (unlikely(t->logs.logwait & LW_REQ)) {
1662 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001663 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 int urilen = msg->sl.rq.l;
1665
1666 if (urilen >= REQURI_LEN)
1667 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001668 memcpy(txn->uri, &req->data[msg->som], urilen);
1669 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001670
1671 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001672 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001673 } else {
1674 Alert("HTTP logging : out of memory.\n");
1675 }
1676 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001677
Willy Tarreau06619262006-12-17 08:37:22 +01001678
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1680 if (unlikely(msg->sl.rq.v_l == 0)) {
1681 int delta;
1682 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001683 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 cur_end = msg->sol + msg->sl.rq.l;
1685 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001686
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001687 if (msg->sl.rq.u_l == 0) {
1688 /* if no URI was set, add "/" */
1689 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1690 cur_end += delta;
1691 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001692 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001693 /* add HTTP version */
1694 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1695 msg->eoh += delta;
1696 cur_end += delta;
1697 cur_end = (char *)http_parse_reqline(msg, req->data,
1698 HTTP_MSG_RQMETH,
1699 msg->sol, cur_end + 1,
1700 NULL, NULL);
1701 if (unlikely(!cur_end))
1702 goto return_bad_req;
1703
1704 /* we have a full HTTP/1.0 request now and we know that
1705 * we have either a CR or an LF at <ptr>.
1706 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001707 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001708 }
1709
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710
1711 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001712 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001713 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001714 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001715
1716 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001717 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001718 * As opposed to version 1.2, now they will be evaluated in the
1719 * filters order and not in the header order. This means that
1720 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001721 *
1722 * We can now check whether we want to switch to another
1723 * backend, in which case we will re-check the backend's
1724 * filters and various options. In order to support 3-level
1725 * switching, here's how we should proceed :
1726 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001727 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001728 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001729 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001730 * There cannot be any switch from there, so ->be cannot be
1731 * changed anymore.
1732 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001733 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001734 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001735 * The response path will be able to apply either ->be, or
1736 * ->be then ->fe filters in order to match the reverse of
1737 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001738 */
1739
Willy Tarreau06619262006-12-17 08:37:22 +01001740 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001741 struct acl_cond *cond;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001742 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001743 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001744
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001745 /* first check whether we have some ACLs set to block this request */
1746 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001747 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001748 if (cond->pol == ACL_COND_UNLESS)
1749 ret = !ret;
1750
1751 if (ret) {
1752 txn->status = 403;
1753 /* let's log the request time */
1754 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
1755 client_retnclose(t, error_message(t, HTTP_ERR_403));
1756 goto return_prx_cond;
1757 }
1758 }
1759
Willy Tarreau06619262006-12-17 08:37:22 +01001760 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01001761 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
1763 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001764 }
1765
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001766 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
1767 /* to ensure correct connection accounting on
1768 * the backend, we count the connection for the
1769 * one managing the queue.
1770 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001771 t->be->beconn++;
1772 if (t->be->beconn > t->be->beconn_max)
1773 t->be->beconn_max = t->be->beconn;
1774 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001775 t->flags |= SN_BE_ASSIGNED;
1776 }
1777
Willy Tarreau06619262006-12-17 08:37:22 +01001778 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01001779 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01001780 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001781 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001782 /* let's log the request time */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001783 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreau80587432006-12-24 17:47:20 +01001784 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001785 goto return_prx_cond;
1786 }
1787
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001788 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001789 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 !(t->flags & SN_CONN_CLOSED)) {
1791 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001792 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01001794
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001795 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001796 old_idx = 0;
1797
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001798 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1799 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001800 cur_ptr = cur_next;
1801 cur_end = cur_ptr + cur_hdr->len;
1802 cur_next = cur_end + cur_hdr->cr + 1;
1803
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001804 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1805 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 /* 3 possibilities :
1807 * - we have already set Connection: close,
1808 * so we remove this line.
1809 * - we have not yet set Connection: close,
1810 * but this line indicates close. We leave
1811 * it untouched and set the flag.
1812 * - we have not yet set Connection: close,
1813 * and this line indicates non-close. We
1814 * replace it.
1815 */
1816 if (t->flags & SN_CONN_CLOSED) {
1817 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001818 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001819 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001820 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1821 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001822 cur_hdr->len = 0;
1823 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001824 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1825 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1826 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001827 cur_next += delta;
1828 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001829 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 }
1831 t->flags |= SN_CONN_CLOSED;
1832 }
1833 }
1834 old_idx = cur_idx;
1835 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02001836 }
1837 /* add request headers from the rule sets in the same order */
1838 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
1839 if (unlikely(http_header_add_tail(req,
1840 &txn->req,
1841 &txn->hdr_idx,
1842 rule_set->req_add[cur_idx])) < 0)
1843 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01001844 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001845
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001846 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01001847 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001848 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01001849 /* we have to check the URI and auth for this request */
1850 if (stats_check_uri_auth(t, rule_set))
1851 return 1;
1852 }
1853
Willy Tarreau55ea7572007-06-17 19:56:27 +02001854 /* now check whether we have some switching rules for this request */
1855 if (!(t->flags & SN_BE_ASSIGNED)) {
1856 struct switching_rule *rule;
1857
1858 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
1859 int ret;
1860
1861 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
1862 if (cond->pol == ACL_COND_UNLESS)
1863 ret = !ret;
1864
1865 if (ret) {
1866 t->be = rule->be.backend;
1867 t->be->beconn++;
1868 if (t->be->beconn > t->be->beconn_max)
1869 t->be->beconn_max = t->be->beconn;
1870 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001871
1872 /* assign new parameters to the session from the new backend */
1873 t->rep->rto = t->req->wto = t->be->srvtimeout;
1874 t->req->cto = t->be->contimeout;
1875 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02001876 t->flags |= SN_BE_ASSIGNED;
1877 break;
1878 }
1879 }
1880 }
1881
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001882 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
1883 /* No backend was set, but there was a default
1884 * backend set in the frontend, so we use it and
1885 * loop again.
1886 */
1887 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001888 t->be->beconn++;
1889 if (t->be->beconn > t->be->beconn_max)
1890 t->be->beconn_max = t->be->beconn;
1891 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001892
1893 /* assign new parameters to the session from the new backend */
1894 t->rep->rto = t->req->wto = t->be->srvtimeout;
1895 t->req->cto = t->be->contimeout;
1896 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001897 t->flags |= SN_BE_ASSIGNED;
1898 }
1899 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01001900
Willy Tarreau58f10d72006-12-04 02:26:12 +01001901
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001902 if (!(t->flags & SN_BE_ASSIGNED)) {
1903 /* To ensure correct connection accounting on
1904 * the backend, we count the connection for the
1905 * one managing the queue.
1906 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001907 t->be->beconn++;
1908 if (t->be->beconn > t->be->beconn_max)
1909 t->be->beconn_max = t->be->beconn;
1910 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001911 t->flags |= SN_BE_ASSIGNED;
1912 }
1913
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001914 /*
1915 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01001916 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001917 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01001918 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01001919 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001920
Willy Tarreau58f10d72006-12-04 02:26:12 +01001921
Willy Tarreau58f10d72006-12-04 02:26:12 +01001922
Willy Tarreau58f10d72006-12-04 02:26:12 +01001923
Willy Tarreau2a324282006-12-05 00:05:46 +01001924 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001925 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01001926 * so let's do the same now.
1927 */
1928
1929 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001930 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001931 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01001932 }
1933
1934
1935 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001936 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01001937 * Note that doing so might move headers in the request, but
1938 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01001939 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01001940 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01001941 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
1942 && !(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 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002968 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
2969 || (t->be->options & PR_O_CHK_CACHE))
2970 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002971
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002972
Willy Tarreaua15645d2007-03-18 16:22:39 +01002973 /*
Willy Tarreau396d2c62007-11-04 19:30:00 +01002974 * 5: check for cache-control or pragma headers if required.
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002975 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002976 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2977 check_response_for_cacheability(t, rep);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002978
2979 /*
2980 * 6: add server cookie in the response if needed
Willy Tarreaua15645d2007-03-18 16:22:39 +01002981 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002982 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2983 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002984 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002985
Willy Tarreaua15645d2007-03-18 16:22:39 +01002986 /* the server is known, it's not the one the client requested, we have to
2987 * insert a set-cookie here, except if we want to insert only on POST
2988 * requests and this one isn't. Note that servers which don't have cookies
2989 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002990 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002991 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002992 t->be->cookie_name,
Willy Tarreaua15645d2007-03-18 16:22:39 +01002993 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002994
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002995 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2996 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002997 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01002998 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002999
Willy Tarreaua15645d2007-03-18 16:22:39 +01003000 /* Here, we will tell an eventual cache on the client side that we don't
3001 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3002 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3003 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3004 */
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003005 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
3006
3007 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3008
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003009 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3010 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003011 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003012 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003013 }
3014
3015
3016 /*
Willy Tarreaua15645d2007-03-18 16:22:39 +01003017 * 7: check if result will be cacheable with a cookie.
3018 * We'll block the response if security checks have caught
3019 * nasty things such as a cacheable cookie.
3020 */
Willy Tarreau3d300592007-03-18 18:34:41 +01003021 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3022 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003023 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003024
Willy Tarreaua15645d2007-03-18 16:22:39 +01003025 /* we're in presence of a cacheable response containing
3026 * a set-cookie header. We'll block it as requested by
3027 * the 'checkcache' option, and send an alert.
3028 */
3029 if (t->srv) {
3030 t->srv->cur_sess--;
3031 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003032 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003033 t->be->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003034
Willy Tarreaua15645d2007-03-18 16:22:39 +01003035 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003036 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003037 send_log(t->be, LOG_ALERT,
3038 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003039 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003040 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003041 }
3042
Willy Tarreaua15645d2007-03-18 16:22:39 +01003043 /*
3044 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02003045 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003046 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02003047 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003048 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02003049 if ((unlikely(msg->sl.st.v_l != 8) ||
3050 unlikely(req->data[msg->som + 7] != '0')) &&
3051 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003052 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003053 goto return_bad_resp;
3054 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003055 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003056
Willy Tarreaubaaee002006-06-26 02:48:02 +02003057
Willy Tarreaua15645d2007-03-18 16:22:39 +01003058 /*************************************************************
3059 * OK, that's finished for the headers. We have done what we *
3060 * could. Let's switch to the DATA state. *
3061 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003062
Willy Tarreaua15645d2007-03-18 16:22:39 +01003063 t->srv_state = SV_STDATA;
3064 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003065 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003066
3067 /* client connection already closed or option 'forceclose' required :
3068 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003069 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003070 if ((req->l == 0) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003071 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003072 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003073 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003074
3075 /* We must ensure that the read part is still alive when switching
3076 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003077 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003078 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003079
Willy Tarreaua15645d2007-03-18 16:22:39 +01003080 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003081 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003082 }
3083
Willy Tarreaua15645d2007-03-18 16:22:39 +01003084#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003085 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003086 /* TCP splicing supported by both FE and BE */
3087 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003088 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003089#endif
3090 /* if the user wants to log as soon as possible, without counting
3091 bytes from the server, then this is the right moment. */
3092 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3093 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01003094 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreau42250582007-04-01 01:30:43 +02003095 if (t->fe->to_log & LW_REQ)
3096 http_sess_log(t);
3097 else
3098 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003099 }
3100
Willy Tarreaua15645d2007-03-18 16:22:39 +01003101 /* Note: we must not try to cheat by jumping directly to DATA,
3102 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003103 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003104
3105 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003106 }
3107 else if (s == SV_STDATA) {
3108 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003109 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02003110 buffer_shutr(rep);
3111 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003112 fd_delete(t->srv_fd);
3113 if (t->srv) {
3114 t->srv->cur_sess--;
3115 t->srv->failed_resp++;
3116 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003117 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003118 t->srv_state = SV_STCLOSE;
3119 if (!(t->flags & SN_ERR_MASK))
3120 t->flags |= SN_ERR_SRVCL;
3121 if (!(t->flags & SN_FINST_MASK))
3122 t->flags |= SN_FINST_D;
3123 /* We used to have a free connection slot. Since we'll never use it,
3124 * we have to inform the server that it may be used by another session.
3125 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003126 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003127 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003128
3129 return 1;
3130 }
3131 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003132 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003133 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003134 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003135 t->srv_state = SV_STSHUTR;
3136 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3137 return 1;
3138 }
3139 /* end of client read and no more data to send */
3140 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003141 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003142 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003143 shutdown(t->srv_fd, SHUT_WR);
3144 /* We must ensure that the read part is still alive when switching
3145 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003146 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003147 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003148
3149 t->srv_state = SV_STSHUTW;
3150 return 1;
3151 }
3152 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003153 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003154 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003155 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003156 t->srv_state = SV_STSHUTR;
3157 if (!(t->flags & SN_ERR_MASK))
3158 t->flags |= SN_ERR_SRVTO;
3159 if (!(t->flags & SN_FINST_MASK))
3160 t->flags |= SN_FINST_D;
3161 return 1;
3162 }
3163 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003164 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003165 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003166 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003167 shutdown(t->srv_fd, SHUT_WR);
3168 /* We must ensure that the read part is still alive when switching
3169 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003170 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003171 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003172 t->srv_state = SV_STSHUTW;
3173 if (!(t->flags & SN_ERR_MASK))
3174 t->flags |= SN_ERR_SRVTO;
3175 if (!(t->flags & SN_FINST_MASK))
3176 t->flags |= SN_FINST_D;
3177 return 1;
3178 }
3179
3180 /* recompute request time-outs */
3181 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003182 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3183 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003184 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003185 }
3186 }
3187 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau66319382007-04-08 17:17:37 +02003188 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3189 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003190 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003191 /* FIXME: to prevent the server from expiring read timeouts during writes,
3192 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003193 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003194 }
3195 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003196 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003197 }
3198 }
3199
3200 /* recompute response time-outs */
3201 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003202 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003203 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003204 }
3205 }
3206 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003207 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003208 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003209 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003210 }
3211 }
3212
3213 return 0; /* other cases change nothing */
3214 }
3215 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003216 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003217 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003218 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003219 fd_delete(t->srv_fd);
3220 if (t->srv) {
3221 t->srv->cur_sess--;
3222 t->srv->failed_resp++;
3223 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003224 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003225 //close(t->srv_fd);
3226 t->srv_state = SV_STCLOSE;
3227 if (!(t->flags & SN_ERR_MASK))
3228 t->flags |= SN_ERR_SRVCL;
3229 if (!(t->flags & SN_FINST_MASK))
3230 t->flags |= SN_FINST_D;
3231 /* We used to have a free connection slot. Since we'll never use it,
3232 * we have to inform the server that it may be used by another session.
3233 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003234 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003235 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003236
3237 return 1;
3238 }
3239 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003240 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003241 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003242 fd_delete(t->srv_fd);
3243 if (t->srv)
3244 t->srv->cur_sess--;
3245 //close(t->srv_fd);
3246 t->srv_state = SV_STCLOSE;
3247 /* We used to have a free connection slot. Since we'll never use it,
3248 * we have to inform the server that it may be used by another session.
3249 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003250 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003251 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003252
3253 return 1;
3254 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003255 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003256 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003257 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003258 fd_delete(t->srv_fd);
3259 if (t->srv)
3260 t->srv->cur_sess--;
3261 //close(t->srv_fd);
3262 t->srv_state = SV_STCLOSE;
3263 if (!(t->flags & SN_ERR_MASK))
3264 t->flags |= SN_ERR_SRVTO;
3265 if (!(t->flags & SN_FINST_MASK))
3266 t->flags |= SN_FINST_D;
3267 /* We used to have a free connection slot. Since we'll never use it,
3268 * we have to inform the server that it may be used by another session.
3269 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003270 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003271 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003272
3273 return 1;
3274 }
3275 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003276 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3277 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003278 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003279 }
3280 }
3281 else { /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02003282 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3283 /* restart writing */
Willy Tarreau33014d02007-06-03 15:25:37 +02003284 if (!tv_add_ifset(&req->wex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003285 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003286 }
3287 }
3288 return 0;
3289 }
3290 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003291 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003292 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003293 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003294 fd_delete(t->srv_fd);
3295 if (t->srv) {
3296 t->srv->cur_sess--;
3297 t->srv->failed_resp++;
3298 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003299 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003300 //close(t->srv_fd);
3301 t->srv_state = SV_STCLOSE;
3302 if (!(t->flags & SN_ERR_MASK))
3303 t->flags |= SN_ERR_SRVCL;
3304 if (!(t->flags & SN_FINST_MASK))
3305 t->flags |= SN_FINST_D;
3306 /* We used to have a free connection slot. Since we'll never use it,
3307 * we have to inform the server that it may be used by another session.
3308 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003309 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003310 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003311
3312 return 1;
3313 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003314 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003315 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003316 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003317 fd_delete(t->srv_fd);
3318 if (t->srv)
3319 t->srv->cur_sess--;
3320 //close(t->srv_fd);
3321 t->srv_state = SV_STCLOSE;
3322 /* We used to have a free connection slot. Since we'll never use it,
3323 * we have to inform the server that it may be used by another session.
3324 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003325 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003326 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003327
3328 return 1;
3329 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003330 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003331 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003332 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003333 fd_delete(t->srv_fd);
3334 if (t->srv)
3335 t->srv->cur_sess--;
3336 //close(t->srv_fd);
3337 t->srv_state = SV_STCLOSE;
3338 if (!(t->flags & SN_ERR_MASK))
3339 t->flags |= SN_ERR_SRVTO;
3340 if (!(t->flags & SN_FINST_MASK))
3341 t->flags |= SN_FINST_D;
3342 /* We used to have a free connection slot. Since we'll never use it,
3343 * we have to inform the server that it may be used by another session.
3344 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003345 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003346 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003347
3348 return 1;
3349 }
3350 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003351 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003352 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003353 }
3354 }
3355 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003356 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003357 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003358 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003359 }
3360 }
3361 return 0;
3362 }
3363 else { /* SV_STCLOSE : nothing to do */
3364 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3365 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003366 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003367 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003368 write(1, trash, len);
3369 }
3370 return 0;
3371 }
3372 return 0;
3373}
3374
3375
3376/*
3377 * Produces data for the session <s> depending on its source. Expects to be
3378 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3379 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3380 * session, which it uses to keep on being called when there is free space in
3381 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3382 * if it changes the session state from CL_STSHUTR, otherwise 0.
3383 */
3384int produce_content(struct session *s)
3385{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003386 if (s->data_source == DATA_SRC_NONE) {
3387 s->flags &= ~SN_SELF_GEN;
3388 return 1;
3389 }
3390 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003391 /* dump server statistics */
Willy Tarreau55bb8452007-10-17 18:44:57 +02003392 int ret = stats_dump_http(s, s->be->uri_auth,
3393 (s->flags & SN_STAT_FMTCSV) ? 0 : STAT_FMT_HTML);
Willy Tarreau91861262007-10-17 17:06:05 +02003394 if (ret >= 0)
3395 return ret;
3396 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003397 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003398
Willy Tarreau91861262007-10-17 17:06:05 +02003399 /* unknown data source or internal error */
3400 s->txn.status = 500;
3401 client_retnclose(s, error_message(s, HTTP_ERR_500));
3402 if (!(s->flags & SN_ERR_MASK))
3403 s->flags |= SN_ERR_PRXCOND;
3404 if (!(s->flags & SN_FINST_MASK))
3405 s->flags |= SN_FINST_R;
3406 s->flags &= ~SN_SELF_GEN;
3407 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003408}
3409
3410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003411/* Iterate the same filter through all request headers.
3412 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003413 * Since it can manage the switch to another backend, it updates the per-proxy
3414 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003415 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003416int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003417{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003418 char term;
3419 char *cur_ptr, *cur_end, *cur_next;
3420 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003421 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003422 struct hdr_idx_elem *cur_hdr;
3423 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003425 last_hdr = 0;
3426
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003427 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003428 old_idx = 0;
3429
3430 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003431 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003432 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003433 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003434 (exp->action == ACT_ALLOW ||
3435 exp->action == ACT_DENY ||
3436 exp->action == ACT_TARPIT))
3437 return 0;
3438
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003439 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003440 if (!cur_idx)
3441 break;
3442
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003443 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003444 cur_ptr = cur_next;
3445 cur_end = cur_ptr + cur_hdr->len;
3446 cur_next = cur_end + cur_hdr->cr + 1;
3447
3448 /* Now we have one header between cur_ptr and cur_end,
3449 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003450 */
3451
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003452 /* The annoying part is that pattern matching needs
3453 * that we modify the contents to null-terminate all
3454 * strings before testing them.
3455 */
3456
3457 term = *cur_end;
3458 *cur_end = '\0';
3459
3460 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3461 switch (exp->action) {
3462 case ACT_SETBE:
3463 /* It is not possible to jump a second time.
3464 * FIXME: should we return an HTTP/500 here so that
3465 * the admin knows there's a problem ?
3466 */
3467 if (t->be != t->fe)
3468 break;
3469
3470 /* Swithing Proxy */
3471 t->be = (struct proxy *) exp->replace;
3472
3473 /* right now, the backend switch is not too much complicated
3474 * because we have associated req_cap and rsp_cap to the
3475 * frontend, and the beconn will be updated later.
3476 */
3477
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003478 t->rep->rto = t->req->wto = t->be->srvtimeout;
3479 t->req->cto = t->be->contimeout;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003480 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003481 last_hdr = 1;
3482 break;
3483
3484 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003485 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003486 last_hdr = 1;
3487 break;
3488
3489 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003490 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003491 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003492 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003493 break;
3494
3495 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003496 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003497 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003498 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003499 break;
3500
3501 case ACT_REPLACE:
3502 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3503 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3504 /* FIXME: if the user adds a newline in the replacement, the
3505 * index will not be recalculated for now, and the new line
3506 * will not be counted as a new header.
3507 */
3508
3509 cur_end += delta;
3510 cur_next += delta;
3511 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003512 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003513 break;
3514
3515 case ACT_REMOVE:
3516 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3517 cur_next += delta;
3518
3519 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003520 txn->req.eoh += delta;
3521 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3522 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003523 cur_hdr->len = 0;
3524 cur_end = NULL; /* null-term has been rewritten */
3525 break;
3526
3527 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003528 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003529 if (cur_end)
3530 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003531
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003532 /* keep the link from this header to next one in case of later
3533 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003534 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003535 old_idx = cur_idx;
3536 }
3537 return 0;
3538}
3539
3540
3541/* Apply the filter to the request line.
3542 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3543 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003544 * Since it can manage the switch to another backend, it updates the per-proxy
3545 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003546 */
3547int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3548{
3549 char term;
3550 char *cur_ptr, *cur_end;
3551 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003552 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003553 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003554
Willy Tarreau58f10d72006-12-04 02:26:12 +01003555
Willy Tarreau3d300592007-03-18 18:34:41 +01003556 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003557 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003558 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003559 (exp->action == ACT_ALLOW ||
3560 exp->action == ACT_DENY ||
3561 exp->action == ACT_TARPIT))
3562 return 0;
3563 else if (exp->action == ACT_REMOVE)
3564 return 0;
3565
3566 done = 0;
3567
Willy Tarreau9cdde232007-05-02 20:58:19 +02003568 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003569 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003570
3571 /* Now we have the request line between cur_ptr and cur_end */
3572
3573 /* The annoying part is that pattern matching needs
3574 * that we modify the contents to null-terminate all
3575 * strings before testing them.
3576 */
3577
3578 term = *cur_end;
3579 *cur_end = '\0';
3580
3581 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3582 switch (exp->action) {
3583 case ACT_SETBE:
3584 /* It is not possible to jump a second time.
3585 * FIXME: should we return an HTTP/500 here so that
3586 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003587 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003588 if (t->be != t->fe)
3589 break;
3590
3591 /* Swithing Proxy */
3592 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003593
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003594 /* right now, the backend switch is not too much complicated
3595 * because we have associated req_cap and rsp_cap to the
3596 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003597 */
3598
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003599 t->rep->rto = t->req->wto = t->be->srvtimeout;
3600 t->req->cto = t->be->contimeout;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003601 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003602 done = 1;
3603 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003604
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003605 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003606 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003607 done = 1;
3608 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003609
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003610 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003611 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003612 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003613 done = 1;
3614 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003615
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003616 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003617 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003618 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003619 done = 1;
3620 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003621
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003622 case ACT_REPLACE:
3623 *cur_end = term; /* restore the string terminator */
3624 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3625 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3626 /* FIXME: if the user adds a newline in the replacement, the
3627 * index will not be recalculated for now, and the new line
3628 * will not be counted as a new header.
3629 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003630
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003631 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003632 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003633
Willy Tarreau9cdde232007-05-02 20:58:19 +02003634 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003635 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003636 HTTP_MSG_RQMETH,
3637 cur_ptr, cur_end + 1,
3638 NULL, NULL);
3639 if (unlikely(!cur_end))
3640 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003642 /* we have a full request and we know that we have either a CR
3643 * or an LF at <ptr>.
3644 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003645 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3646 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003647 /* there is no point trying this regex on headers */
3648 return 1;
3649 }
3650 }
3651 *cur_end = term; /* restore the string terminator */
3652 return done;
3653}
Willy Tarreau97de6242006-12-27 17:18:38 +01003654
Willy Tarreau58f10d72006-12-04 02:26:12 +01003655
Willy Tarreau58f10d72006-12-04 02:26:12 +01003656
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003657/*
3658 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3659 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003660 * unparsable request. Since it can manage the switch to another backend, it
3661 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003662 */
3663int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3664{
Willy Tarreau3d300592007-03-18 18:34:41 +01003665 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003666 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003667 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003668 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003669
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003670 /*
3671 * The interleaving of transformations and verdicts
3672 * makes it difficult to decide to continue or stop
3673 * the evaluation.
3674 */
3675
Willy Tarreau3d300592007-03-18 18:34:41 +01003676 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003677 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3678 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3679 exp = exp->next;
3680 continue;
3681 }
3682
3683 /* Apply the filter to the request line. */
3684 ret = apply_filter_to_req_line(t, req, exp);
3685 if (unlikely(ret < 0))
3686 return -1;
3687
3688 if (likely(ret == 0)) {
3689 /* The filter did not match the request, it can be
3690 * iterated through all headers.
3691 */
3692 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003693 }
3694 exp = exp->next;
3695 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003696 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003697}
3698
3699
Willy Tarreaua15645d2007-03-18 16:22:39 +01003700
Willy Tarreau58f10d72006-12-04 02:26:12 +01003701/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003702 * Manage client-side cookie. It can impact performance by about 2% so it is
3703 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003704 */
3705void manage_client_side_cookies(struct session *t, struct buffer *req)
3706{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003707 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003708 char *p1, *p2, *p3, *p4;
3709 char *del_colon, *del_cookie, *colon;
3710 int app_cookies;
3711
3712 appsess *asession_temp = NULL;
3713 appsess local_asession;
3714
3715 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003716 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003717
Willy Tarreau2a324282006-12-05 00:05:46 +01003718 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003719 * we start with the start line.
3720 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003721 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003722 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003723
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003724 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003725 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003726 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003727
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003728 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003729 cur_ptr = cur_next;
3730 cur_end = cur_ptr + cur_hdr->len;
3731 cur_next = cur_end + cur_hdr->cr + 1;
3732
3733 /* We have one full header between cur_ptr and cur_end, and the
3734 * next header starts at cur_next. We're only interested in
3735 * "Cookie:" headers.
3736 */
3737
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003738 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3739 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003740 old_idx = cur_idx;
3741 continue;
3742 }
3743
3744 /* Now look for cookies. Conforming to RFC2109, we have to support
3745 * attributes whose name begin with a '$', and associate them with
3746 * the right cookie, if we want to delete this cookie.
3747 * So there are 3 cases for each cookie read :
3748 * 1) it's a special attribute, beginning with a '$' : ignore it.
3749 * 2) it's a server id cookie that we *MAY* want to delete : save
3750 * some pointers on it (last semi-colon, beginning of cookie...)
3751 * 3) it's an application cookie : we *MAY* have to delete a previous
3752 * "special" cookie.
3753 * At the end of loop, if a "special" cookie remains, we may have to
3754 * remove it. If no application cookie persists in the header, we
3755 * *MUST* delete it
3756 */
3757
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003758 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003759
Willy Tarreau58f10d72006-12-04 02:26:12 +01003760 /* del_cookie == NULL => nothing to be deleted */
3761 del_colon = del_cookie = NULL;
3762 app_cookies = 0;
3763
3764 while (p1 < cur_end) {
3765 /* skip spaces and colons, but keep an eye on these ones */
3766 while (p1 < cur_end) {
3767 if (*p1 == ';' || *p1 == ',')
3768 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003769 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003770 break;
3771 p1++;
3772 }
3773
3774 if (p1 == cur_end)
3775 break;
3776
3777 /* p1 is at the beginning of the cookie name */
3778 p2 = p1;
3779 while (p2 < cur_end && *p2 != '=')
3780 p2++;
3781
3782 if (p2 == cur_end)
3783 break;
3784
3785 p3 = p2 + 1; /* skips the '=' sign */
3786 if (p3 == cur_end)
3787 break;
3788
3789 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003790 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003791 p4++;
3792
3793 /* here, we have the cookie name between p1 and p2,
3794 * and its value between p3 and p4.
3795 * we can process it :
3796 *
3797 * Cookie: NAME=VALUE;
3798 * | || || |
3799 * | || || +--> p4
3800 * | || |+-------> p3
3801 * | || +--------> p2
3802 * | |+------------> p1
3803 * | +-------------> colon
3804 * +--------------------> cur_ptr
3805 */
3806
3807 if (*p1 == '$') {
3808 /* skip this one */
3809 }
3810 else {
3811 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003812 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003813 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003814 (p4 - p1 >= t->fe->capture_namelen) &&
3815 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003816 int log_len = p4 - p1;
3817
Willy Tarreau086b3b42007-05-13 21:45:51 +02003818 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003819 Alert("HTTP logging : out of memory.\n");
3820 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003821 if (log_len > t->fe->capture_len)
3822 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003823 memcpy(txn->cli_cookie, p1, log_len);
3824 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003825 }
3826 }
3827
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003828 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3829 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003830 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003831 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003832 char *delim;
3833
3834 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3835 * have the server ID betweek p3 and delim, and the original cookie between
3836 * delim+1 and p4. Otherwise, delim==p4 :
3837 *
3838 * Cookie: NAME=SRV~VALUE;
3839 * | || || | |
3840 * | || || | +--> p4
3841 * | || || +--------> delim
3842 * | || |+-----------> p3
3843 * | || +------------> p2
3844 * | |+----------------> p1
3845 * | +-----------------> colon
3846 * +------------------------> cur_ptr
3847 */
3848
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003849 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003850 for (delim = p3; delim < p4; delim++)
3851 if (*delim == COOKIE_DELIM)
3852 break;
3853 }
3854 else
3855 delim = p4;
3856
3857
3858 /* Here, we'll look for the first running server which supports the cookie.
3859 * This allows to share a same cookie between several servers, for example
3860 * to dedicate backup servers to specific servers only.
3861 * However, to prevent clients from sticking to cookie-less backup server
3862 * when they have incidentely learned an empty cookie, we simply ignore
3863 * empty cookies and mark them as invalid.
3864 */
3865 if (delim == p3)
3866 srv = NULL;
3867
3868 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01003869 if (srv->cookie && (srv->cklen == delim - p3) &&
3870 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003871 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003872 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003873 txn->flags &= ~TX_CK_MASK;
3874 txn->flags |= TX_CK_VALID;
3875 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003876 t->srv = srv;
3877 break;
3878 } else {
3879 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01003880 txn->flags &= ~TX_CK_MASK;
3881 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003882 }
3883 }
3884 srv = srv->next;
3885 }
3886
Willy Tarreau3d300592007-03-18 18:34:41 +01003887 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003888 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01003889 txn->flags &= ~TX_CK_MASK;
3890 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003891 }
3892
3893 /* depending on the cookie mode, we may have to either :
3894 * - delete the complete cookie if we're in insert+indirect mode, so that
3895 * the server never sees it ;
3896 * - remove the server id from the cookie value, and tag the cookie as an
3897 * application cookie so that it does not get accidentely removed later,
3898 * if we're in cookie prefix mode
3899 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003900 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003901 int delta; /* negative */
3902
3903 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
3904 p4 += delta;
3905 cur_end += delta;
3906 cur_next += delta;
3907 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003908 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003909
3910 del_cookie = del_colon = NULL;
3911 app_cookies++; /* protect the header from deletion */
3912 }
3913 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003914 (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 +01003915 del_cookie = p1;
3916 del_colon = colon;
3917 }
3918 } else {
3919 /* now we know that we must keep this cookie since it's
3920 * not ours. But if we wanted to delete our cookie
3921 * earlier, we cannot remove the complete header, but we
3922 * can remove the previous block itself.
3923 */
3924 app_cookies++;
3925
3926 if (del_cookie != NULL) {
3927 int delta; /* negative */
3928
3929 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
3930 p4 += delta;
3931 cur_end += delta;
3932 cur_next += delta;
3933 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003934 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003935 del_cookie = del_colon = NULL;
3936 }
3937 }
3938
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003939 if ((t->be->appsession_name != NULL) &&
3940 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003941 /* first, let's see if the cookie is our appcookie*/
3942
3943 /* Cool... it's the right one */
3944
3945 asession_temp = &local_asession;
3946
Willy Tarreau63963c62007-05-13 21:29:55 +02003947 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003948 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3949 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3950 return;
3951 }
3952
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003953 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
3954 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003955 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02003956
Willy Tarreau58f10d72006-12-04 02:26:12 +01003957 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02003958 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
3959 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02003960 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003961 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003962 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003963 Alert("Not enough memory process_cli():asession:calloc().\n");
3964 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
3965 return;
3966 }
3967
3968 asession_temp->sessid = local_asession.sessid;
3969 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02003970 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003971 } else {
3972 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003973 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003974 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003975 if (asession_temp->serverid == NULL) {
3976 Alert("Found Application Session without matching server.\n");
3977 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003978 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003979 while (srv) {
3980 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003981 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003982 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003983 txn->flags &= ~TX_CK_MASK;
3984 txn->flags |= TX_CK_VALID;
3985 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003986 t->srv = srv;
3987 break;
3988 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01003989 txn->flags &= ~TX_CK_MASK;
3990 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003991 }
3992 }
3993 srv = srv->next;
3994 }/* end while(srv) */
3995 }/* end else if server == NULL */
3996
Willy Tarreaud825eef2007-05-12 22:35:00 +02003997 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003998 }/* end if ((t->proxy->appsession_name != NULL) ... */
3999 }
4000
4001 /* we'll have to look for another cookie ... */
4002 p1 = p4;
4003 } /* while (p1 < cur_end) */
4004
4005 /* There's no more cookie on this line.
4006 * We may have marked the last one(s) for deletion.
4007 * We must do this now in two ways :
4008 * - if there is no app cookie, we simply delete the header ;
4009 * - if there are app cookies, we must delete the end of the
4010 * string properly, including the colon/semi-colon before
4011 * the cookie name.
4012 */
4013 if (del_cookie != NULL) {
4014 int delta;
4015 if (app_cookies) {
4016 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4017 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004018 cur_hdr->len += delta;
4019 } else {
4020 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004021
4022 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004023 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4024 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004025 cur_hdr->len = 0;
4026 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004027 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004028 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004029 }
4030
4031 /* keep the link from this header to next one */
4032 old_idx = cur_idx;
4033 } /* end of cookie processing on this header */
4034}
4035
4036
Willy Tarreaua15645d2007-03-18 16:22:39 +01004037/* Iterate the same filter through all response headers contained in <rtr>.
4038 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4039 */
4040int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4041{
4042 char term;
4043 char *cur_ptr, *cur_end, *cur_next;
4044 int cur_idx, old_idx, last_hdr;
4045 struct http_txn *txn = &t->txn;
4046 struct hdr_idx_elem *cur_hdr;
4047 int len, delta;
4048
4049 last_hdr = 0;
4050
4051 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4052 old_idx = 0;
4053
4054 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004055 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004056 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004057 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004058 (exp->action == ACT_ALLOW ||
4059 exp->action == ACT_DENY))
4060 return 0;
4061
4062 cur_idx = txn->hdr_idx.v[old_idx].next;
4063 if (!cur_idx)
4064 break;
4065
4066 cur_hdr = &txn->hdr_idx.v[cur_idx];
4067 cur_ptr = cur_next;
4068 cur_end = cur_ptr + cur_hdr->len;
4069 cur_next = cur_end + cur_hdr->cr + 1;
4070
4071 /* Now we have one header between cur_ptr and cur_end,
4072 * and the next header starts at cur_next.
4073 */
4074
4075 /* The annoying part is that pattern matching needs
4076 * that we modify the contents to null-terminate all
4077 * strings before testing them.
4078 */
4079
4080 term = *cur_end;
4081 *cur_end = '\0';
4082
4083 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4084 switch (exp->action) {
4085 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004086 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004087 last_hdr = 1;
4088 break;
4089
4090 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004091 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004092 last_hdr = 1;
4093 break;
4094
4095 case ACT_REPLACE:
4096 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4097 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4098 /* FIXME: if the user adds a newline in the replacement, the
4099 * index will not be recalculated for now, and the new line
4100 * will not be counted as a new header.
4101 */
4102
4103 cur_end += delta;
4104 cur_next += delta;
4105 cur_hdr->len += delta;
4106 txn->rsp.eoh += delta;
4107 break;
4108
4109 case ACT_REMOVE:
4110 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4111 cur_next += delta;
4112
4113 /* FIXME: this should be a separate function */
4114 txn->rsp.eoh += delta;
4115 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4116 txn->hdr_idx.used--;
4117 cur_hdr->len = 0;
4118 cur_end = NULL; /* null-term has been rewritten */
4119 break;
4120
4121 }
4122 }
4123 if (cur_end)
4124 *cur_end = term; /* restore the string terminator */
4125
4126 /* keep the link from this header to next one in case of later
4127 * removal of next header.
4128 */
4129 old_idx = cur_idx;
4130 }
4131 return 0;
4132}
4133
4134
4135/* Apply the filter to the status line in the response buffer <rtr>.
4136 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4137 * or -1 if a replacement resulted in an invalid status line.
4138 */
4139int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4140{
4141 char term;
4142 char *cur_ptr, *cur_end;
4143 int done;
4144 struct http_txn *txn = &t->txn;
4145 int len, delta;
4146
4147
Willy Tarreau3d300592007-03-18 18:34:41 +01004148 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004149 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004150 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004151 (exp->action == ACT_ALLOW ||
4152 exp->action == ACT_DENY))
4153 return 0;
4154 else if (exp->action == ACT_REMOVE)
4155 return 0;
4156
4157 done = 0;
4158
Willy Tarreau9cdde232007-05-02 20:58:19 +02004159 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004160 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4161
4162 /* Now we have the status line between cur_ptr and cur_end */
4163
4164 /* The annoying part is that pattern matching needs
4165 * that we modify the contents to null-terminate all
4166 * strings before testing them.
4167 */
4168
4169 term = *cur_end;
4170 *cur_end = '\0';
4171
4172 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4173 switch (exp->action) {
4174 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004175 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004176 done = 1;
4177 break;
4178
4179 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004180 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004181 done = 1;
4182 break;
4183
4184 case ACT_REPLACE:
4185 *cur_end = term; /* restore the string terminator */
4186 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4187 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4188 /* FIXME: if the user adds a newline in the replacement, the
4189 * index will not be recalculated for now, and the new line
4190 * will not be counted as a new header.
4191 */
4192
4193 txn->rsp.eoh += delta;
4194 cur_end += delta;
4195
Willy Tarreau9cdde232007-05-02 20:58:19 +02004196 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004197 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004198 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004199 cur_ptr, cur_end + 1,
4200 NULL, NULL);
4201 if (unlikely(!cur_end))
4202 return -1;
4203
4204 /* we have a full respnse and we know that we have either a CR
4205 * or an LF at <ptr>.
4206 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004207 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004208 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4209 /* there is no point trying this regex on headers */
4210 return 1;
4211 }
4212 }
4213 *cur_end = term; /* restore the string terminator */
4214 return done;
4215}
4216
4217
4218
4219/*
4220 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4221 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4222 * unparsable response.
4223 */
4224int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4225{
Willy Tarreau3d300592007-03-18 18:34:41 +01004226 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004227 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004228 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004229 int ret;
4230
4231 /*
4232 * The interleaving of transformations and verdicts
4233 * makes it difficult to decide to continue or stop
4234 * the evaluation.
4235 */
4236
Willy Tarreau3d300592007-03-18 18:34:41 +01004237 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004238 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4239 exp->action == ACT_PASS)) {
4240 exp = exp->next;
4241 continue;
4242 }
4243
4244 /* Apply the filter to the status line. */
4245 ret = apply_filter_to_sts_line(t, rtr, exp);
4246 if (unlikely(ret < 0))
4247 return -1;
4248
4249 if (likely(ret == 0)) {
4250 /* The filter did not match the response, it can be
4251 * iterated through all headers.
4252 */
4253 apply_filter_to_resp_headers(t, rtr, exp);
4254 }
4255 exp = exp->next;
4256 }
4257 return 0;
4258}
4259
4260
4261
4262/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004263 * Manage server-side cookies. It can impact performance by about 2% so it is
4264 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004265 */
4266void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4267{
4268 struct http_txn *txn = &t->txn;
4269 char *p1, *p2, *p3, *p4;
4270
4271 appsess *asession_temp = NULL;
4272 appsess local_asession;
4273
4274 char *cur_ptr, *cur_end, *cur_next;
4275 int cur_idx, old_idx, delta;
4276
Willy Tarreaua15645d2007-03-18 16:22:39 +01004277 /* Iterate through the headers.
4278 * we start with the start line.
4279 */
4280 old_idx = 0;
4281 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4282
4283 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4284 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004285 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004286
4287 cur_hdr = &txn->hdr_idx.v[cur_idx];
4288 cur_ptr = cur_next;
4289 cur_end = cur_ptr + cur_hdr->len;
4290 cur_next = cur_end + cur_hdr->cr + 1;
4291
4292 /* We have one full header between cur_ptr and cur_end, and the
4293 * next header starts at cur_next. We're only interested in
4294 * "Cookie:" headers.
4295 */
4296
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004297 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4298 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004299 old_idx = cur_idx;
4300 continue;
4301 }
4302
4303 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004304 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004305
4306
4307 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004308 if (t->be->cookie_name == NULL &&
4309 t->be->appsession_name == NULL &&
4310 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004311 return;
4312
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004313 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004314
4315 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004316 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4317 break;
4318
4319 /* p1 is at the beginning of the cookie name */
4320 p2 = p1;
4321
4322 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4323 p2++;
4324
4325 if (p2 == cur_end || *p2 == ';') /* next cookie */
4326 break;
4327
4328 p3 = p2 + 1; /* skip the '=' sign */
4329 if (p3 == cur_end)
4330 break;
4331
4332 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004333 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004334 p4++;
4335
4336 /* here, we have the cookie name between p1 and p2,
4337 * and its value between p3 and p4.
4338 * we can process it.
4339 */
4340
4341 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004342 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004343 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004344 (p4 - p1 >= t->be->capture_namelen) &&
4345 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004346 int log_len = p4 - p1;
4347
Willy Tarreau086b3b42007-05-13 21:45:51 +02004348 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004349 Alert("HTTP logging : out of memory.\n");
4350 }
4351
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004352 if (log_len > t->be->capture_len)
4353 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004354 memcpy(txn->srv_cookie, p1, log_len);
4355 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004356 }
4357
4358 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004359 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4360 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004361 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004362 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004363
4364 /* If the cookie is in insert mode on a known server, we'll delete
4365 * this occurrence because we'll insert another one later.
4366 * We'll delete it too if the "indirect" option is set and we're in
4367 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004368 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4369 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004370 /* this header must be deleted */
4371 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4372 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4373 txn->hdr_idx.used--;
4374 cur_hdr->len = 0;
4375 cur_next += delta;
4376 txn->rsp.eoh += delta;
4377
Willy Tarreau3d300592007-03-18 18:34:41 +01004378 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004379 }
4380 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004381 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004382 /* replace bytes p3->p4 with the cookie name associated
4383 * with this server since we know it.
4384 */
4385 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4386 cur_hdr->len += delta;
4387 cur_next += delta;
4388 txn->rsp.eoh += delta;
4389
Willy Tarreau3d300592007-03-18 18:34:41 +01004390 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004391 }
4392 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004393 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004394 /* insert the cookie name associated with this server
4395 * before existing cookie, and insert a delimitor between them..
4396 */
4397 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4398 cur_hdr->len += delta;
4399 cur_next += delta;
4400 txn->rsp.eoh += delta;
4401
4402 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004403 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004404 }
4405 }
4406 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004407 else if ((t->be->appsession_name != NULL) &&
4408 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004409
4410 /* Cool... it's the right one */
4411
4412 size_t server_id_len = strlen(t->srv->id) + 1;
4413 asession_temp = &local_asession;
4414
Willy Tarreau63963c62007-05-13 21:29:55 +02004415 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004416 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4417 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4418 return;
4419 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004420 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4421 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004422 asession_temp->serverid = NULL;
4423
4424 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004425 if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004426 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004427 Alert("Not enough Memory process_srv():asession:calloc().\n");
4428 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4429 return;
4430 }
4431 asession_temp->sessid = local_asession.sessid;
4432 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02004433 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4434 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004435 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004436 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004437 }
4438
Willy Tarreaua15645d2007-03-18 16:22:39 +01004439 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004440 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004441 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4442 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4443 return;
4444 }
4445 asession_temp->serverid[0] = '\0';
4446 }
4447
4448 if (asession_temp->serverid[0] == '\0')
4449 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4450
Willy Tarreaud825eef2007-05-12 22:35:00 +02004451 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004452
4453#if defined(DEBUG_HASH)
Willy Tarreau51041c72007-09-09 21:56:53 +02004454 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004455#endif
4456 }/* end if ((t->proxy->appsession_name != NULL) ... */
4457 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4458 } /* we're now at the end of the cookie value */
4459
4460 /* keep the link from this header to next one */
4461 old_idx = cur_idx;
4462 } /* end of cookie processing on this header */
4463}
4464
4465
4466
4467/*
4468 * Check if response is cacheable or not. Updates t->flags.
4469 */
4470void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4471{
4472 struct http_txn *txn = &t->txn;
4473 char *p1, *p2;
4474
4475 char *cur_ptr, *cur_end, *cur_next;
4476 int cur_idx;
4477
Willy Tarreau3d300592007-03-18 18:34:41 +01004478 if (!txn->flags & TX_CACHEABLE)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004479 return;
4480
4481 /* Iterate through the headers.
4482 * we start with the start line.
4483 */
4484 cur_idx = 0;
4485 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4486
4487 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4488 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004489 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004490
4491 cur_hdr = &txn->hdr_idx.v[cur_idx];
4492 cur_ptr = cur_next;
4493 cur_end = cur_ptr + cur_hdr->len;
4494 cur_next = cur_end + cur_hdr->cr + 1;
4495
4496 /* We have one full header between cur_ptr and cur_end, and the
4497 * next header starts at cur_next. We're only interested in
4498 * "Cookie:" headers.
4499 */
4500
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004501 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4502 if (val) {
4503 if ((cur_end - (cur_ptr + val) >= 8) &&
4504 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4505 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4506 return;
4507 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004508 }
4509
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004510 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4511 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004512 continue;
4513
4514 /* OK, right now we know we have a cache-control header at cur_ptr */
4515
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004516 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004517
4518 if (p1 >= cur_end) /* no more info */
4519 continue;
4520
4521 /* p1 is at the beginning of the value */
4522 p2 = p1;
4523
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004524 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004525 p2++;
4526
4527 /* we have a complete value between p1 and p2 */
4528 if (p2 < cur_end && *p2 == '=') {
4529 /* we have something of the form no-cache="set-cookie" */
4530 if ((cur_end - p1 >= 21) &&
4531 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4532 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004533 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004534 continue;
4535 }
4536
4537 /* OK, so we know that either p2 points to the end of string or to a comma */
4538 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4539 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4540 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4541 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004542 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004543 return;
4544 }
4545
4546 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004547 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004548 continue;
4549 }
4550 }
4551}
4552
4553
Willy Tarreau58f10d72006-12-04 02:26:12 +01004554/*
4555 * Try to retrieve a known appsession in the URI, then the associated server.
4556 * If the server is found, it's assigned to the session.
4557 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004558void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004559{
Willy Tarreau3d300592007-03-18 18:34:41 +01004560 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004561 appsess *asession_temp = NULL;
4562 appsess local_asession;
4563 char *request_line;
4564
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004565 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004566 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004567 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004568 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004569 return;
4570
4571 /* skip ';' */
4572 request_line++;
4573
4574 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004575 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004576 return;
4577
4578 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004579 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004580
4581 /* First try if we already have an appsession */
4582 asession_temp = &local_asession;
4583
Willy Tarreau63963c62007-05-13 21:29:55 +02004584 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004585 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4586 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4587 return;
4588 }
4589
4590 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004591 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4592 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004593 asession_temp->serverid = NULL;
4594
4595 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004596 if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004597 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004598 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004599 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004600 Alert("Not enough memory process_cli():asession:calloc().\n");
4601 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4602 return;
4603 }
4604 asession_temp->sessid = local_asession.sessid;
4605 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02004606 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004607 }
4608 else {
4609 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004610 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004611 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004612
Willy Tarreaud825eef2007-05-12 22:35:00 +02004613 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004614 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004615
Willy Tarreau58f10d72006-12-04 02:26:12 +01004616#if defined(DEBUG_HASH)
Willy Tarreau51041c72007-09-09 21:56:53 +02004617 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004618#endif
4619 if (asession_temp->serverid == NULL) {
4620 Alert("Found Application Session without matching server.\n");
4621 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004622 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004623 while (srv) {
4624 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004625 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004626 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004627 txn->flags &= ~TX_CK_MASK;
4628 txn->flags |= TX_CK_VALID;
4629 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004630 t->srv = srv;
4631 break;
4632 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004633 txn->flags &= ~TX_CK_MASK;
4634 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004635 }
4636 }
4637 srv = srv->next;
4638 }
4639 }
4640}
4641
4642
Willy Tarreaub2513902006-12-17 14:52:38 +01004643/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004644 * In a GET or HEAD request, check if the requested URI matches the stats uri
4645 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004646 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004647 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004648 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004649 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004650 *
4651 * Returns 1 if the session's state changes, otherwise 0.
4652 */
4653int stats_check_uri_auth(struct session *t, struct proxy *backend)
4654{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004655 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004656 struct uri_auth *uri_auth = backend->uri_auth;
4657 struct user_auth *user;
4658 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004659 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004660
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004661 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004662 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004663 return 0;
4664
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004665 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004666
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004667 /* the URI is in h */
4668 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004669 return 0;
4670
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004671 h += uri_auth->uri_len;
4672 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4673 if (memcmp(h, ";up", 3) == 0) {
4674 t->flags |= SN_STAT_HIDEDWN;
4675 break;
4676 }
4677 h++;
4678 }
4679
4680 if (uri_auth->refresh) {
4681 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4682 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4683 if (memcmp(h, ";norefresh", 10) == 0) {
4684 t->flags |= SN_STAT_NORFRSH;
4685 break;
4686 }
4687 h++;
4688 }
4689 }
4690
Willy Tarreau55bb8452007-10-17 18:44:57 +02004691 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4692 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4693 if (memcmp(h, ";csv", 4) == 0) {
4694 t->flags |= SN_STAT_FMTCSV;
4695 break;
4696 }
4697 h++;
4698 }
4699
Willy Tarreaub2513902006-12-17 14:52:38 +01004700 /* we are in front of a interceptable URI. Let's check
4701 * if there's an authentication and if it's valid.
4702 */
4703 user = uri_auth->users;
4704 if (!user) {
4705 /* no user auth required, it's OK */
4706 authenticated = 1;
4707 } else {
4708 authenticated = 0;
4709
4710 /* a user list is defined, we have to check.
4711 * skip 21 chars for "Authorization: Basic ".
4712 */
4713
4714 /* FIXME: this should move to an earlier place */
4715 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004716 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4717 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4718 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004719 if (len > 14 &&
4720 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004721 txn->auth_hdr.str = h;
4722 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004723 break;
4724 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004725 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004726 }
4727
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004728 if (txn->auth_hdr.len < 21 ||
4729 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004730 user = NULL;
4731
4732 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004733 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4734 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004735 user->user_pwd, user->user_len)) {
4736 authenticated = 1;
4737 break;
4738 }
4739 user = user->next;
4740 }
4741 }
4742
4743 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004744 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004745
4746 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004747 msg.str = trash;
4748 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004749 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01004750 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01004751 if (!(t->flags & SN_ERR_MASK))
4752 t->flags |= SN_ERR_PRXCOND;
4753 if (!(t->flags & SN_FINST_MASK))
4754 t->flags |= SN_FINST_R;
4755 return 1;
4756 }
4757
4758 /* The request is valid, the user is authenticate. Let's start sending
4759 * data.
4760 */
4761 t->cli_state = CL_STSHUTR;
4762 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02004763 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub2513902006-12-17 14:52:38 +01004764 t->data_source = DATA_SRC_STATS;
4765 t->data_state = DATA_ST_INIT;
4766 produce_content(t);
4767 return 1;
4768}
4769
4770
Willy Tarreaubaaee002006-06-26 02:48:02 +02004771/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004772 * Print a debug line with a header
4773 */
4774void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4775{
4776 int len, max;
4777 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
4778 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
4779 max = end - start;
4780 UBOUND(max, sizeof(trash) - len - 1);
4781 len += strlcpy2(trash + len, start, max + 1);
4782 trash[len++] = '\n';
4783 write(1, trash, len);
4784}
4785
4786
Willy Tarreau8797c062007-05-07 00:55:35 +02004787/************************************************************************/
4788/* The code below is dedicated to ACL parsing and matching */
4789/************************************************************************/
4790
4791
4792
4793
4794/* 1. Check on METHOD
4795 * We use the pre-parsed method if it is known, and store its number as an
4796 * integer. If it is unknown, we use the pointer and the length.
4797 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004798static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004799{
4800 int len, meth;
4801
Willy Tarreauae8b7962007-06-09 23:10:04 +02004802 len = strlen(*text);
4803 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004804
4805 pattern->val.i = meth;
4806 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004807 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004808 if (!pattern->ptr.str)
4809 return 0;
4810 pattern->len = len;
4811 }
4812 return 1;
4813}
4814
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004815static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004816acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4817 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004818{
4819 int meth;
4820 struct http_txn *txn = l7;
4821
Willy Tarreauc11416f2007-06-17 16:58:38 +02004822 if (txn->req.msg_state != HTTP_MSG_BODY)
4823 return 0;
4824
Willy Tarreau8797c062007-05-07 00:55:35 +02004825 meth = txn->meth;
4826 test->i = meth;
4827 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004828 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4829 /* ensure the indexes are not affected */
4830 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004831 test->len = txn->req.sl.rq.m_l;
4832 test->ptr = txn->req.sol;
4833 }
4834 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4835 return 1;
4836}
4837
4838static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4839{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004840 int icase;
4841
Willy Tarreau8797c062007-05-07 00:55:35 +02004842 if (test->i != pattern->val.i)
4843 return 0;
4844
4845 if (test->i != HTTP_METH_OTHER)
4846 return 1;
4847
4848 /* Other method, we must compare the strings */
4849 if (pattern->len != test->len)
4850 return 0;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004851
4852 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4853 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4854 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau8797c062007-05-07 00:55:35 +02004855 return 0;
4856 return 1;
4857}
4858
4859/* 2. Check on Request/Status Version
4860 * We simply compare strings here.
4861 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004862static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004863{
Willy Tarreauae8b7962007-06-09 23:10:04 +02004864 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004865 if (!pattern->ptr.str)
4866 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02004867 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004868 return 1;
4869}
4870
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004871static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004872acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
4873 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004874{
4875 struct http_txn *txn = l7;
4876 char *ptr;
4877 int len;
4878
Willy Tarreauc11416f2007-06-17 16:58:38 +02004879 if (txn->req.msg_state != HTTP_MSG_BODY)
4880 return 0;
4881
Willy Tarreau8797c062007-05-07 00:55:35 +02004882 len = txn->req.sl.rq.v_l;
4883 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
4884
4885 while ((len-- > 0) && (*ptr++ != '/'));
4886 if (len <= 0)
4887 return 0;
4888
4889 test->ptr = ptr;
4890 test->len = len;
4891
4892 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4893 return 1;
4894}
4895
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004896static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004897acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
4898 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004899{
4900 struct http_txn *txn = l7;
4901 char *ptr;
4902 int len;
4903
Willy Tarreauc11416f2007-06-17 16:58:38 +02004904 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4905 return 0;
4906
Willy Tarreau8797c062007-05-07 00:55:35 +02004907 len = txn->rsp.sl.st.v_l;
4908 ptr = txn->rsp.sol;
4909
4910 while ((len-- > 0) && (*ptr++ != '/'));
4911 if (len <= 0)
4912 return 0;
4913
4914 test->ptr = ptr;
4915 test->len = len;
4916
4917 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4918 return 1;
4919}
4920
4921/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004922static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004923acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
4924 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004925{
4926 struct http_txn *txn = l7;
4927 char *ptr;
4928 int len;
4929
Willy Tarreauc11416f2007-06-17 16:58:38 +02004930 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4931 return 0;
4932
Willy Tarreau8797c062007-05-07 00:55:35 +02004933 len = txn->rsp.sl.st.c_l;
4934 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
4935
4936 test->i = __strl2ui(ptr, len);
4937 test->flags = ACL_TEST_F_VOL_1ST;
4938 return 1;
4939}
4940
4941/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004942static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004943acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
4944 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004945{
4946 struct http_txn *txn = l7;
4947
Willy Tarreauc11416f2007-06-17 16:58:38 +02004948 if (txn->req.msg_state != HTTP_MSG_BODY)
4949 return 0;
4950 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4951 /* ensure the indexes are not affected */
4952 return 0;
4953
Willy Tarreau8797c062007-05-07 00:55:35 +02004954 test->len = txn->req.sl.rq.u_l;
4955 test->ptr = txn->req.sol + txn->req.sl.rq.u;
4956
Willy Tarreauf3d25982007-05-08 22:45:09 +02004957 /* we do not need to set READ_ONLY because the data is in a buffer */
4958 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02004959 return 1;
4960}
4961
Willy Tarreauc11416f2007-06-17 16:58:38 +02004962/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
4963 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
4964 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02004965static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004966acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004967 struct acl_expr *expr, struct acl_test *test)
4968{
4969 struct http_txn *txn = l7;
4970 struct hdr_idx *idx = &txn->hdr_idx;
4971 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004972
4973 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4974 /* search for header from the beginning */
4975 ctx->idx = 0;
4976
Willy Tarreau33a7e692007-06-10 19:45:56 +02004977 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4978 test->flags |= ACL_TEST_F_FETCH_MORE;
4979 test->flags |= ACL_TEST_F_VOL_HDR;
4980 test->len = ctx->vlen;
4981 test->ptr = (char *)ctx->line + ctx->val;
4982 return 1;
4983 }
4984
4985 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4986 test->flags |= ACL_TEST_F_VOL_HDR;
4987 return 0;
4988}
4989
Willy Tarreau33a7e692007-06-10 19:45:56 +02004990static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004991acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
4992 struct acl_expr *expr, struct acl_test *test)
4993{
4994 struct http_txn *txn = l7;
4995
4996 if (txn->req.msg_state != HTTP_MSG_BODY)
4997 return 0;
4998 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4999 /* ensure the indexes are not affected */
5000 return 0;
5001
5002 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5003}
5004
5005static int
5006acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5007 struct acl_expr *expr, struct acl_test *test)
5008{
5009 struct http_txn *txn = l7;
5010
5011 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5012 return 0;
5013
5014 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5015}
5016
5017/* 6. Check on HTTP header count. The number of occurrences is returned.
5018 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5019 */
5020static int
5021acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005022 struct acl_expr *expr, struct acl_test *test)
5023{
5024 struct http_txn *txn = l7;
5025 struct hdr_idx *idx = &txn->hdr_idx;
5026 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005027 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005028
Willy Tarreau33a7e692007-06-10 19:45:56 +02005029 ctx.idx = 0;
5030 cnt = 0;
5031 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5032 cnt++;
5033
5034 test->i = cnt;
5035 test->flags = ACL_TEST_F_VOL_HDR;
5036 return 1;
5037}
5038
Willy Tarreauc11416f2007-06-17 16:58:38 +02005039static int
5040acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5041 struct acl_expr *expr, struct acl_test *test)
5042{
5043 struct http_txn *txn = l7;
5044
5045 if (txn->req.msg_state != HTTP_MSG_BODY)
5046 return 0;
5047 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5048 /* ensure the indexes are not affected */
5049 return 0;
5050
5051 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5052}
5053
5054static int
5055acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5056 struct acl_expr *expr, struct acl_test *test)
5057{
5058 struct http_txn *txn = l7;
5059
5060 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5061 return 0;
5062
5063 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5064}
5065
Willy Tarreau33a7e692007-06-10 19:45:56 +02005066/* 7. Check on HTTP header's integer value. The integer value is returned.
5067 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005068 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005069 */
5070static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005071acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005072 struct acl_expr *expr, struct acl_test *test)
5073{
5074 struct http_txn *txn = l7;
5075 struct hdr_idx *idx = &txn->hdr_idx;
5076 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005077
5078 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5079 /* search for header from the beginning */
5080 ctx->idx = 0;
5081
Willy Tarreau33a7e692007-06-10 19:45:56 +02005082 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5083 test->flags |= ACL_TEST_F_FETCH_MORE;
5084 test->flags |= ACL_TEST_F_VOL_HDR;
5085 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5086 return 1;
5087 }
5088
5089 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5090 test->flags |= ACL_TEST_F_VOL_HDR;
5091 return 0;
5092}
5093
Willy Tarreauc11416f2007-06-17 16:58:38 +02005094static int
5095acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5096 struct acl_expr *expr, struct acl_test *test)
5097{
5098 struct http_txn *txn = l7;
5099
5100 if (txn->req.msg_state != HTTP_MSG_BODY)
5101 return 0;
5102 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5103 /* ensure the indexes are not affected */
5104 return 0;
5105
5106 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5107}
5108
5109static int
5110acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5111 struct acl_expr *expr, struct acl_test *test)
5112{
5113 struct http_txn *txn = l7;
5114
5115 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5116 return 0;
5117
5118 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5119}
5120
Willy Tarreau737b0c12007-06-10 21:28:46 +02005121/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5122 * the first '/' after the possible hostname, and ends before the possible '?'.
5123 */
5124static int
5125acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5126 struct acl_expr *expr, struct acl_test *test)
5127{
5128 struct http_txn *txn = l7;
5129 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005130
Willy Tarreauc11416f2007-06-17 16:58:38 +02005131 if (txn->req.msg_state != HTTP_MSG_BODY)
5132 return 0;
5133 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5134 /* ensure the indexes are not affected */
5135 return 0;
5136
Willy Tarreau737b0c12007-06-10 21:28:46 +02005137 ptr = txn->req.sol + txn->req.sl.rq.u;
5138 end = ptr + txn->req.sl.rq.u_l;
5139
5140 if (ptr >= end)
5141 return 0;
5142
5143 /* RFC2616, par. 5.1.2 :
5144 * Request-URI = "*" | absuri | abspath | authority
5145 */
5146
5147 if (*ptr == '*')
5148 return 0;
5149
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005150 if (isalpha((unsigned char)*ptr)) {
Willy Tarreau737b0c12007-06-10 21:28:46 +02005151 /* this is a scheme as described by RFC3986, par. 3.1 */
5152 ptr++;
5153 while (ptr < end &&
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005154 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
Willy Tarreau737b0c12007-06-10 21:28:46 +02005155 ptr++;
5156 /* skip '://' */
5157 if (ptr == end || *ptr++ != ':')
5158 return 0;
5159 if (ptr == end || *ptr++ != '/')
5160 return 0;
5161 if (ptr == end || *ptr++ != '/')
5162 return 0;
5163 }
5164 /* skip [user[:passwd]@]host[:[port]] */
5165
5166 while (ptr < end && *ptr != '/')
5167 ptr++;
5168
5169 if (ptr == end)
5170 return 0;
5171
5172 /* OK, we got the '/' ! */
5173 test->ptr = ptr;
5174
5175 while (ptr < end && *ptr != '?')
5176 ptr++;
5177
5178 test->len = ptr - test->ptr;
5179
5180 /* we do not need to set READ_ONLY because the data is in a buffer */
5181 test->flags = ACL_TEST_F_VOL_1ST;
5182 return 1;
5183}
5184
5185
Willy Tarreau8797c062007-05-07 00:55:35 +02005186
5187/************************************************************************/
5188/* All supported keywords must be declared here. */
5189/************************************************************************/
5190
5191/* Note: must not be declared <const> as its list will be overwritten */
5192static struct acl_kw_list acl_kws = {{ },{
5193 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth },
5194 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str },
5195 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str },
Willy Tarreauae8b7962007-06-09 23:10:04 +02005196 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int },
Willy Tarreau8797c062007-05-07 00:55:35 +02005197
Willy Tarreauae8b7962007-06-09 23:10:04 +02005198 { "url", acl_parse_str, acl_fetch_url, acl_match_str },
5199 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg },
5200 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end },
5201 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub },
5202 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir },
5203 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom },
5204 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg },
Willy Tarreau8797c062007-05-07 00:55:35 +02005205
Willy Tarreauc11416f2007-06-17 16:58:38 +02005206 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str },
5207 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg },
5208 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg },
5209 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end },
5210 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub },
5211 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir },
5212 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom },
5213 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int },
5214 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int },
5215
5216 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str },
5217 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg },
5218 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg },
5219 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end },
5220 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub },
5221 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir },
5222 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom },
5223 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int },
5224 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005225
5226 { "path", acl_parse_str, acl_fetch_path, acl_match_str },
5227 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg },
5228 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg },
5229 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end },
5230 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub },
5231 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir },
5232 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom },
5233
Willy Tarreauf3d25982007-05-08 22:45:09 +02005234 { NULL, NULL, NULL, NULL },
5235
5236#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005237 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5238 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5239 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5240 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5241 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5242 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5243 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5244
Willy Tarreau8797c062007-05-07 00:55:35 +02005245 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5246 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5247 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5248 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5249 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5250 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5251 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5252 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5253
5254 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5255 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5256 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5257 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5258 { NULL, NULL, NULL, NULL },
5259#endif
5260}};
5261
5262
5263__attribute__((constructor))
5264static void __http_protocol_init(void)
5265{
5266 acl_register_keywords(&acl_kws);
5267}
5268
5269
Willy Tarreau58f10d72006-12-04 02:26:12 +01005270/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005271 * Local variables:
5272 * c-indent-level: 8
5273 * c-basic-offset: 8
5274 * End:
5275 */