blob: 3f8e0ac88da59cef6b7e218c5dbf454d424ee400 [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 Tarreau21d2af32008-02-14 20:25:24 +0100595/* Parse the URI from the given transaction (which is assumed to be in request
596 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
597 * It is returned otherwise.
598 */
599static char *
600http_get_path(struct http_txn *txn)
601{
602 char *ptr, *end;
603
604 ptr = txn->req.sol + txn->req.sl.rq.u;
605 end = ptr + txn->req.sl.rq.u_l;
606
607 if (ptr >= end)
608 return NULL;
609
610 /* RFC2616, par. 5.1.2 :
611 * Request-URI = "*" | absuri | abspath | authority
612 */
613
614 if (*ptr == '*')
615 return NULL;
616
617 if (isalpha((unsigned char)*ptr)) {
618 /* this is a scheme as described by RFC3986, par. 3.1 */
619 ptr++;
620 while (ptr < end &&
621 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
622 ptr++;
623 /* skip '://' */
624 if (ptr == end || *ptr++ != ':')
625 return NULL;
626 if (ptr == end || *ptr++ != '/')
627 return NULL;
628 if (ptr == end || *ptr++ != '/')
629 return NULL;
630 }
631 /* skip [user[:passwd]@]host[:[port]] */
632
633 while (ptr < end && *ptr != '/')
634 ptr++;
635
636 if (ptr == end)
637 return NULL;
638
639 /* OK, we got the '/' ! */
640 return ptr;
641}
642
Willy Tarreaubaaee002006-06-26 02:48:02 +0200643/* Processes the client and server jobs of a session task, then
644 * puts it back to the wait queue in a clean state, or
645 * cleans up its resources if it must be deleted. Returns
646 * the time the task accepts to wait, or TIME_ETERNITY for
647 * infinity.
648 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200649void process_session(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200650{
651 struct session *s = t->context;
652 int fsm_resync = 0;
653
654 do {
655 fsm_resync = 0;
656 //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
657 fsm_resync |= process_cli(s);
658 //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
659 fsm_resync |= process_srv(s);
660 //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
661 } while (fsm_resync);
662
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200663 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100664
665 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
666 session_process_counters(s);
667
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200668 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
669 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200671 tv_min(&t->expire, &s->req->rex, &s->req->wex);
672 tv_bound(&t->expire, &s->req->cex);
673 tv_bound(&t->expire, &s->rep->rex);
674 tv_bound(&t->expire, &s->rep->wex);
Willy Tarreau036fae02008-01-06 13:24:40 +0100675 if (s->cli_state == CL_STHEADERS)
676 tv_bound(&t->expire, &s->txn.exp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200677
678 /* restore t to its place in the task list */
679 task_queue(t);
680
681#ifdef DEBUG_FULL
682 /* DEBUG code : this should never ever happen, otherwise it indicates
683 * that a task still has something to do and will provoke a quick loop.
684 */
Willy Tarreaub8816082008-01-18 12:18:15 +0100685 if (tv_ms_remain2(&now, &t->expire) <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200686 exit(100);
687#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200688 *next = t->expire;
689 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200690 }
691
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100692 s->fe->feconn--;
693 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200694 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695 actconn--;
696
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200697 if (unlikely((global.mode & MODE_DEBUG) &&
698 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200699 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100700 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200701 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100702 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200703 write(1, trash, len);
704 }
705
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200706 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100707 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200708
709 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200710 if (s->logs.logwait &&
711 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200712 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
713 if (s->fe->to_log & LW_REQ)
714 http_sess_log(s);
715 else
716 tcp_sess_log(s);
717 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200718
719 /* the task MUST not be in the run queue anymore */
720 task_delete(t);
721 session_free(s);
722 task_free(t);
Willy Tarreaud825eef2007-05-12 22:35:00 +0200723 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200724}
725
726
Willy Tarreau42250582007-04-01 01:30:43 +0200727extern const char sess_term_cond[8];
728extern const char sess_fin_state[8];
729extern const char *monthname[12];
730const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
731const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
732 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
733 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200734struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200735struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100736
Willy Tarreau42250582007-04-01 01:30:43 +0200737/*
738 * send a log for the session when we have enough info about it.
739 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100740 */
Willy Tarreau42250582007-04-01 01:30:43 +0200741static void http_sess_log(struct session *s)
742{
743 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
744 struct proxy *fe = s->fe;
745 struct proxy *be = s->be;
746 struct proxy *prx_log;
747 struct http_txn *txn = &s->txn;
748 int tolog;
749 char *uri, *h;
750 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200751 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200752 static char tmpline[MAX_SYSLOG_LEN];
753 int hdr;
754
755 if (fe->logfac1 < 0 && fe->logfac2 < 0)
756 return;
757 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100758
Willy Tarreau42250582007-04-01 01:30:43 +0200759 if (s->cli_addr.ss_family == AF_INET)
760 inet_ntop(AF_INET,
761 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
762 pn, sizeof(pn));
763 else
764 inet_ntop(AF_INET6,
765 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
766 pn, sizeof(pn));
767
Willy Tarreaufe944602007-10-25 10:34:16 +0200768 get_localtime(s->logs.tv_accept.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200769
770 /* FIXME: let's limit ourselves to frontend logging for now. */
771 tolog = fe->to_log;
772
773 h = tmpline;
774 if (fe->to_log & LW_REQHDR &&
775 txn->req.cap &&
776 (h < tmpline + sizeof(tmpline) - 10)) {
777 *(h++) = ' ';
778 *(h++) = '{';
779 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
780 if (hdr)
781 *(h++) = '|';
782 if (txn->req.cap[hdr] != NULL)
783 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
784 '#', hdr_encode_map, txn->req.cap[hdr]);
785 }
786 *(h++) = '}';
787 }
788
789 if (fe->to_log & LW_RSPHDR &&
790 txn->rsp.cap &&
791 (h < tmpline + sizeof(tmpline) - 7)) {
792 *(h++) = ' ';
793 *(h++) = '{';
794 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
795 if (hdr)
796 *(h++) = '|';
797 if (txn->rsp.cap[hdr] != NULL)
798 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
799 '#', hdr_encode_map, txn->rsp.cap[hdr]);
800 }
801 *(h++) = '}';
802 }
803
804 if (h < tmpline + sizeof(tmpline) - 4) {
805 *(h++) = ' ';
806 *(h++) = '"';
807 uri = txn->uri ? txn->uri : "<BADREQ>";
808 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
809 '#', url_encode_map, uri);
810 *(h++) = '"';
811 }
812 *h = '\0';
813
814 svid = (tolog & LW_SVID) ?
815 (s->data_source != DATA_SRC_STATS) ?
816 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
817
818 send_log(prx_log, LOG_INFO,
819 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
820 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100821 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200822 pn,
823 (s->cli_addr.ss_family == AF_INET) ?
824 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
825 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200826 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
827 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.tv_accept.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200828 fe->id, be->id, svid,
829 s->logs.t_request,
830 (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1,
831 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
832 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
833 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
834 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100835 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200836 txn->cli_cookie ? txn->cli_cookie : "-",
837 txn->srv_cookie ? txn->srv_cookie : "-",
838 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
839 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
840 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
841 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
842 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100843 (s->flags & SN_REDISP)?"+":"",
844 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200845 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
846
847 s->logs.logwait = 0;
848}
849
Willy Tarreau117f59e2007-03-04 18:17:17 +0100850
851/*
852 * Capture headers from message starting at <som> according to header list
853 * <cap_hdr>, and fill the <idx> structure appropriately.
854 */
855void capture_headers(char *som, struct hdr_idx *idx,
856 char **cap, struct cap_hdr *cap_hdr)
857{
858 char *eol, *sol, *col, *sov;
859 int cur_idx;
860 struct cap_hdr *h;
861 int len;
862
863 sol = som + hdr_idx_first_pos(idx);
864 cur_idx = hdr_idx_first_idx(idx);
865
866 while (cur_idx) {
867 eol = sol + idx->v[cur_idx].len;
868
869 col = sol;
870 while (col < eol && *col != ':')
871 col++;
872
873 sov = col + 1;
874 while (sov < eol && http_is_lws[(unsigned char)*sov])
875 sov++;
876
877 for (h = cap_hdr; h; h = h->next) {
878 if ((h->namelen == col - sol) &&
879 (strncasecmp(sol, h->name, h->namelen) == 0)) {
880 if (cap[h->index] == NULL)
881 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200882 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100883
884 if (cap[h->index] == NULL) {
885 Alert("HTTP capture : out of memory.\n");
886 continue;
887 }
888
889 len = eol - sov;
890 if (len > h->len)
891 len = h->len;
892
893 memcpy(cap[h->index], sov, len);
894 cap[h->index][len]=0;
895 }
896 }
897 sol = eol + idx->v[cur_idx].cr + 1;
898 cur_idx = idx->v[cur_idx].next;
899 }
900}
901
902
Willy Tarreau42250582007-04-01 01:30:43 +0200903/* either we find an LF at <ptr> or we jump to <bad>.
904 */
905#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
906
907/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
908 * otherwise to <http_msg_ood> with <state> set to <st>.
909 */
910#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
911 ptr++; \
912 if (likely(ptr < end)) \
913 goto good; \
914 else { \
915 state = (st); \
916 goto http_msg_ood; \
917 } \
918 } while (0)
919
920
Willy Tarreaubaaee002006-06-26 02:48:02 +0200921/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100922 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100923 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
924 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
925 * will give undefined results.
926 * Note that it is upon the caller's responsibility to ensure that ptr < end,
927 * and that msg->sol points to the beginning of the response.
928 * If a complete line is found (which implies that at least one CR or LF is
929 * found before <end>, the updated <ptr> is returned, otherwise NULL is
930 * returned indicating an incomplete line (which does not mean that parts have
931 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
932 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
933 * upon next call.
934 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200935 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100936 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
937 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200938 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100939 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100940const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
941 unsigned int state, const char *ptr, const char *end,
942 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100943{
944 __label__
945 http_msg_rpver,
946 http_msg_rpver_sp,
947 http_msg_rpcode,
948 http_msg_rpcode_sp,
949 http_msg_rpreason,
950 http_msg_rpline_eol,
951 http_msg_ood, /* out of data */
952 http_msg_invalid;
953
954 switch (state) {
955 http_msg_rpver:
956 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100957 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100958 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
959
960 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100961 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100962 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
963 }
964 goto http_msg_invalid;
965
966 http_msg_rpver_sp:
967 case HTTP_MSG_RPVER_SP:
968 if (likely(!HTTP_IS_LWS(*ptr))) {
969 msg->sl.st.c = ptr - msg_buf;
970 goto http_msg_rpcode;
971 }
972 if (likely(HTTP_IS_SPHT(*ptr)))
973 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
974 /* so it's a CR/LF, this is invalid */
975 goto http_msg_invalid;
976
977 http_msg_rpcode:
978 case HTTP_MSG_RPCODE:
979 if (likely(!HTTP_IS_LWS(*ptr)))
980 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
981
982 if (likely(HTTP_IS_SPHT(*ptr))) {
983 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
984 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
985 }
986
987 /* so it's a CR/LF, so there is no reason phrase */
988 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
989 http_msg_rsp_reason:
990 /* FIXME: should we support HTTP responses without any reason phrase ? */
991 msg->sl.st.r = ptr - msg_buf;
992 msg->sl.st.r_l = 0;
993 goto http_msg_rpline_eol;
994
995 http_msg_rpcode_sp:
996 case HTTP_MSG_RPCODE_SP:
997 if (likely(!HTTP_IS_LWS(*ptr))) {
998 msg->sl.st.r = ptr - msg_buf;
999 goto http_msg_rpreason;
1000 }
1001 if (likely(HTTP_IS_SPHT(*ptr)))
1002 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1003 /* so it's a CR/LF, so there is no reason phrase */
1004 goto http_msg_rsp_reason;
1005
1006 http_msg_rpreason:
1007 case HTTP_MSG_RPREASON:
1008 if (likely(!HTTP_IS_CRLF(*ptr)))
1009 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1010 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1011 http_msg_rpline_eol:
1012 /* We have seen the end of line. Note that we do not
1013 * necessarily have the \n yet, but at least we know that we
1014 * have EITHER \r OR \n, otherwise the response would not be
1015 * complete. We can then record the response length and return
1016 * to the caller which will be able to register it.
1017 */
1018 msg->sl.st.l = ptr - msg->sol;
1019 return ptr;
1020
1021#ifdef DEBUG_FULL
1022 default:
1023 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1024 exit(1);
1025#endif
1026 }
1027
1028 http_msg_ood:
1029 /* out of data */
1030 if (ret_state)
1031 *ret_state = state;
1032 if (ret_ptr)
1033 *ret_ptr = (char *)ptr;
1034 return NULL;
1035
1036 http_msg_invalid:
1037 /* invalid message */
1038 if (ret_state)
1039 *ret_state = HTTP_MSG_ERROR;
1040 return NULL;
1041}
1042
1043
1044/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001045 * This function parses a request line between <ptr> and <end>, starting with
1046 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1047 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1048 * will give undefined results.
1049 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1050 * and that msg->sol points to the beginning of the request.
1051 * If a complete line is found (which implies that at least one CR or LF is
1052 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1053 * returned indicating an incomplete line (which does not mean that parts have
1054 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1055 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1056 * upon next call.
1057 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001058 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001059 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1060 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001061 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001062 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001063const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1064 unsigned int state, const char *ptr, const char *end,
1065 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001066{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001067 __label__
1068 http_msg_rqmeth,
1069 http_msg_rqmeth_sp,
1070 http_msg_rquri,
1071 http_msg_rquri_sp,
1072 http_msg_rqver,
1073 http_msg_rqline_eol,
1074 http_msg_ood, /* out of data */
1075 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 switch (state) {
1078 http_msg_rqmeth:
1079 case HTTP_MSG_RQMETH:
1080 if (likely(HTTP_IS_TOKEN(*ptr)))
1081 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001084 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001085 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1086 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001087
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 if (likely(HTTP_IS_CRLF(*ptr))) {
1089 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001090 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001091 http_msg_req09_uri:
1092 msg->sl.rq.u = ptr - msg_buf;
1093 http_msg_req09_uri_e:
1094 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1095 http_msg_req09_ver:
1096 msg->sl.rq.v = ptr - msg_buf;
1097 msg->sl.rq.v_l = 0;
1098 goto http_msg_rqline_eol;
1099 }
1100 goto http_msg_invalid;
1101
1102 http_msg_rqmeth_sp:
1103 case HTTP_MSG_RQMETH_SP:
1104 if (likely(!HTTP_IS_LWS(*ptr))) {
1105 msg->sl.rq.u = ptr - msg_buf;
1106 goto http_msg_rquri;
1107 }
1108 if (likely(HTTP_IS_SPHT(*ptr)))
1109 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1110 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1111 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001112
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001113 http_msg_rquri:
1114 case HTTP_MSG_RQURI:
1115 if (likely(!HTTP_IS_LWS(*ptr)))
1116 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001117
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001118 if (likely(HTTP_IS_SPHT(*ptr))) {
1119 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1120 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1121 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001122
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001123 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1124 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001125
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001126 http_msg_rquri_sp:
1127 case HTTP_MSG_RQURI_SP:
1128 if (likely(!HTTP_IS_LWS(*ptr))) {
1129 msg->sl.rq.v = ptr - msg_buf;
1130 goto http_msg_rqver;
1131 }
1132 if (likely(HTTP_IS_SPHT(*ptr)))
1133 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1134 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1135 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001136
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001137 http_msg_rqver:
1138 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001139 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001140 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001141
1142 if (likely(HTTP_IS_CRLF(*ptr))) {
1143 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1144 http_msg_rqline_eol:
1145 /* We have seen the end of line. Note that we do not
1146 * necessarily have the \n yet, but at least we know that we
1147 * have EITHER \r OR \n, otherwise the request would not be
1148 * complete. We can then record the request length and return
1149 * to the caller which will be able to register it.
1150 */
1151 msg->sl.rq.l = ptr - msg->sol;
1152 return ptr;
1153 }
1154
1155 /* neither an HTTP_VER token nor a CRLF */
1156 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001157
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001158#ifdef DEBUG_FULL
1159 default:
1160 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1161 exit(1);
1162#endif
1163 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001164
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001165 http_msg_ood:
1166 /* out of data */
1167 if (ret_state)
1168 *ret_state = state;
1169 if (ret_ptr)
1170 *ret_ptr = (char *)ptr;
1171 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001172
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001173 http_msg_invalid:
1174 /* invalid message */
1175 if (ret_state)
1176 *ret_state = HTTP_MSG_ERROR;
1177 return NULL;
1178}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001179
1180
Willy Tarreau8973c702007-01-21 23:58:29 +01001181/*
1182 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001183 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001184 * when data are missing and recalled at the exact same location with no
1185 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001186 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1187 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001188 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001189void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1190{
1191 __label__
1192 http_msg_rqbefore,
1193 http_msg_rqbefore_cr,
1194 http_msg_rqmeth,
1195 http_msg_rqline_end,
1196 http_msg_hdr_first,
1197 http_msg_hdr_name,
1198 http_msg_hdr_l1_sp,
1199 http_msg_hdr_l1_lf,
1200 http_msg_hdr_l1_lws,
1201 http_msg_hdr_val,
1202 http_msg_hdr_l2_lf,
1203 http_msg_hdr_l2_lws,
1204 http_msg_complete_header,
1205 http_msg_last_lf,
1206 http_msg_ood, /* out of data */
1207 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001208
Willy Tarreaue69eada2008-01-27 00:34:10 +01001209 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001210 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001211
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001212 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001213 ptr = buf->lr;
1214 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001215
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001216 if (unlikely(ptr >= end))
1217 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001218
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001219 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001220 /*
1221 * First, states that are specific to the response only.
1222 * We check them first so that request and headers are
1223 * closer to each other (accessed more often).
1224 */
1225 http_msg_rpbefore:
1226 case HTTP_MSG_RPBEFORE:
1227 if (likely(HTTP_IS_TOKEN(*ptr))) {
1228 if (likely(ptr == buf->data)) {
1229 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001230 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001231 } else {
1232#if PARSE_PRESERVE_EMPTY_LINES
1233 /* only skip empty leading lines, don't remove them */
1234 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001235 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001236#else
1237 /* Remove empty leading lines, as recommended by
1238 * RFC2616. This takes a lot of time because we
1239 * must move all the buffer backwards, but this
1240 * is rarely needed. The method above will be
1241 * cleaner when we'll be able to start sending
1242 * the request from any place in the buffer.
1243 */
1244 buf->lr = ptr;
1245 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001246 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001247 msg->sol = buf->data;
1248 ptr = buf->data;
1249 end = buf->r;
1250#endif
1251 }
1252 hdr_idx_init(idx);
1253 state = HTTP_MSG_RPVER;
1254 goto http_msg_rpver;
1255 }
1256
1257 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1258 goto http_msg_invalid;
1259
1260 if (unlikely(*ptr == '\n'))
1261 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1262 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1263 /* stop here */
1264
1265 http_msg_rpbefore_cr:
1266 case HTTP_MSG_RPBEFORE_CR:
1267 EXPECT_LF_HERE(ptr, http_msg_invalid);
1268 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1269 /* stop here */
1270
1271 http_msg_rpver:
1272 case HTTP_MSG_RPVER:
1273 case HTTP_MSG_RPVER_SP:
1274 case HTTP_MSG_RPCODE:
1275 case HTTP_MSG_RPCODE_SP:
1276 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001277 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001278 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001279 if (unlikely(!ptr))
1280 return;
1281
1282 /* we have a full response and we know that we have either a CR
1283 * or an LF at <ptr>.
1284 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001285 //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 +01001286 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1287
1288 msg->sol = ptr;
1289 if (likely(*ptr == '\r'))
1290 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1291 goto http_msg_rpline_end;
1292
1293 http_msg_rpline_end:
1294 case HTTP_MSG_RPLINE_END:
1295 /* msg->sol must point to the first of CR or LF. */
1296 EXPECT_LF_HERE(ptr, http_msg_invalid);
1297 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1298 /* stop here */
1299
1300 /*
1301 * Second, states that are specific to the request only
1302 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001303 http_msg_rqbefore:
1304 case HTTP_MSG_RQBEFORE:
1305 if (likely(HTTP_IS_TOKEN(*ptr))) {
1306 if (likely(ptr == buf->data)) {
1307 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001308 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001309 } else {
1310#if PARSE_PRESERVE_EMPTY_LINES
1311 /* only skip empty leading lines, don't remove them */
1312 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001313 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001314#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001315 /* Remove empty leading lines, as recommended by
1316 * RFC2616. This takes a lot of time because we
1317 * must move all the buffer backwards, but this
1318 * is rarely needed. The method above will be
1319 * cleaner when we'll be able to start sending
1320 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001321 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001322 buf->lr = ptr;
1323 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001324 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325 msg->sol = buf->data;
1326 ptr = buf->data;
1327 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001328#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001329 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001330 /* we will need this when keep-alive will be supported
1331 hdr_idx_init(idx);
1332 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001333 state = HTTP_MSG_RQMETH;
1334 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001335 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001336
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1338 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001339
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 if (unlikely(*ptr == '\n'))
1341 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1342 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001343 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001344
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001345 http_msg_rqbefore_cr:
1346 case HTTP_MSG_RQBEFORE_CR:
1347 EXPECT_LF_HERE(ptr, http_msg_invalid);
1348 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001350
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001351 http_msg_rqmeth:
1352 case HTTP_MSG_RQMETH:
1353 case HTTP_MSG_RQMETH_SP:
1354 case HTTP_MSG_RQURI:
1355 case HTTP_MSG_RQURI_SP:
1356 case HTTP_MSG_RQVER:
1357 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001358 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001359 if (unlikely(!ptr))
1360 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001361
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001362 /* we have a full request and we know that we have either a CR
1363 * or an LF at <ptr>.
1364 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001365 //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 +01001366 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001367
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001368 msg->sol = ptr;
1369 if (likely(*ptr == '\r'))
1370 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001372
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001373 http_msg_rqline_end:
1374 case HTTP_MSG_RQLINE_END:
1375 /* check for HTTP/0.9 request : no version information available.
1376 * msg->sol must point to the first of CR or LF.
1377 */
1378 if (unlikely(msg->sl.rq.v_l == 0))
1379 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 EXPECT_LF_HERE(ptr, http_msg_invalid);
1382 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001383 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001384
Willy Tarreau8973c702007-01-21 23:58:29 +01001385 /*
1386 * Common states below
1387 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 http_msg_hdr_first:
1389 case HTTP_MSG_HDR_FIRST:
1390 msg->sol = ptr;
1391 if (likely(!HTTP_IS_CRLF(*ptr))) {
1392 goto http_msg_hdr_name;
1393 }
1394
1395 if (likely(*ptr == '\r'))
1396 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1397 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001398
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001399 http_msg_hdr_name:
1400 case HTTP_MSG_HDR_NAME:
1401 /* assumes msg->sol points to the first char */
1402 if (likely(HTTP_IS_TOKEN(*ptr)))
1403 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001404
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 if (likely(*ptr == ':')) {
1406 msg->col = ptr - buf->data;
1407 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1408 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001409
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001410 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 http_msg_hdr_l1_sp:
1413 case HTTP_MSG_HDR_L1_SP:
1414 /* assumes msg->sol points to the first char and msg->col to the colon */
1415 if (likely(HTTP_IS_SPHT(*ptr)))
1416 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001417
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 /* header value can be basically anything except CR/LF */
1419 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001420
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 if (likely(!HTTP_IS_CRLF(*ptr))) {
1422 goto http_msg_hdr_val;
1423 }
1424
1425 if (likely(*ptr == '\r'))
1426 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1427 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 http_msg_hdr_l1_lf:
1430 case HTTP_MSG_HDR_L1_LF:
1431 EXPECT_LF_HERE(ptr, http_msg_invalid);
1432 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001433
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 http_msg_hdr_l1_lws:
1435 case HTTP_MSG_HDR_L1_LWS:
1436 if (likely(HTTP_IS_SPHT(*ptr))) {
1437 /* replace HT,CR,LF with spaces */
1438 for (; buf->data+msg->sov < ptr; msg->sov++)
1439 buf->data[msg->sov] = ' ';
1440 goto http_msg_hdr_l1_sp;
1441 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001442 /* we had a header consisting only in spaces ! */
1443 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001444 goto http_msg_complete_header;
1445
1446 http_msg_hdr_val:
1447 case HTTP_MSG_HDR_VAL:
1448 /* assumes msg->sol points to the first char, msg->col to the
1449 * colon, and msg->sov points to the first character of the
1450 * value.
1451 */
1452 if (likely(!HTTP_IS_CRLF(*ptr)))
1453 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 msg->eol = ptr;
1456 /* Note: we could also copy eol into ->eoh so that we have the
1457 * real header end in case it ends with lots of LWS, but is this
1458 * really needed ?
1459 */
1460 if (likely(*ptr == '\r'))
1461 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1462 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 http_msg_hdr_l2_lf:
1465 case HTTP_MSG_HDR_L2_LF:
1466 EXPECT_LF_HERE(ptr, http_msg_invalid);
1467 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001468
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 http_msg_hdr_l2_lws:
1470 case HTTP_MSG_HDR_L2_LWS:
1471 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1472 /* LWS: replace HT,CR,LF with spaces */
1473 for (; msg->eol < ptr; msg->eol++)
1474 *msg->eol = ' ';
1475 goto http_msg_hdr_val;
1476 }
1477 http_msg_complete_header:
1478 /*
1479 * It was a new header, so the last one is finished.
1480 * Assumes msg->sol points to the first char, msg->col to the
1481 * colon, msg->sov points to the first character of the value
1482 * and msg->eol to the first CR or LF so we know how the line
1483 * ends. We insert last header into the index.
1484 */
1485 /*
1486 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1487 write(2, msg->sol, msg->eol-msg->sol);
1488 fprintf(stderr,"\n");
1489 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1492 idx, idx->tail) < 0))
1493 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001494
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001495 msg->sol = ptr;
1496 if (likely(!HTTP_IS_CRLF(*ptr))) {
1497 goto http_msg_hdr_name;
1498 }
1499
1500 if (likely(*ptr == '\r'))
1501 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1502 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001503
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001504 http_msg_last_lf:
1505 case HTTP_MSG_LAST_LF:
1506 /* Assumes msg->sol points to the first of either CR or LF */
1507 EXPECT_LF_HERE(ptr, http_msg_invalid);
1508 ptr++;
1509 buf->lr = ptr;
1510 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001511 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 return;
1513#ifdef DEBUG_FULL
1514 default:
1515 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1516 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001517#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 }
1519 http_msg_ood:
1520 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001521 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 buf->lr = ptr;
1523 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 http_msg_invalid:
1526 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001527 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 return;
1529}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001530
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531/*
1532 * manages the client FSM and its socket. BTW, it also tries to handle the
1533 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1534 * 0 else.
1535 */
1536int process_cli(struct session *t)
1537{
1538 int s = t->srv_state;
1539 int c = t->cli_state;
1540 struct buffer *req = t->req;
1541 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001542
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
1544 cli_stnames[c], srv_stnames[s],
Willy Tarreauf161a342007-04-08 16:59:42 +02001545 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 req->rex.tv_sec, req->rex.tv_usec,
1547 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001548
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 if (c == CL_STHEADERS) {
1550 /*
1551 * Now parse the partial (or complete) lines.
1552 * We will check the request syntax, and also join multi-line
1553 * headers. An index of all the lines will be elaborated while
1554 * parsing.
1555 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001556 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001559 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 * req->data + req->eoh = end of processed headers / start of current one
1561 * req->data + req->eol = end of current header or line (LF or CRLF)
1562 * req->lr = first non-visited byte
1563 * req->r = end of data
1564 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001565
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001567 struct http_txn *txn = &t->txn;
1568 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001572 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001573
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 /* 1: we might have to print this header in debug mode */
1575 if (unlikely((global.mode & MODE_DEBUG) &&
1576 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001577 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001579
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001580 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 eol = sol + msg->sl.rq.l;
1582 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001583
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001584 sol += hdr_idx_first_pos(&txn->hdr_idx);
1585 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001586
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001588 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001590 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1591 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 }
1593 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001594
Willy Tarreau58f10d72006-12-04 02:26:12 +01001595
1596 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001598 * If not so, we check the FD and buffer states before leaving.
1599 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1601 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001602 *
1603 */
1604
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001605 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 /*
1607 * First, let's catch bad requests.
1608 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001609 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001611
1612 /* 1: Since we are in header mode, if there's no space
1613 * left for headers, we won't be able to free more
1614 * later, so the session will never terminate. We
1615 * must terminate it now.
1616 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001617 if (unlikely(req->l >= req->rlim - req->data)) {
1618 /* FIXME: check if URI is set and return Status
1619 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001620 */
Willy Tarreau06619262006-12-17 08:37:22 +01001621 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001622 }
1623
1624 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1626 /* read error, or last read : give up. */
Willy Tarreaufa645582007-06-03 15:59:52 +02001627 buffer_shutr(req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001628 fd_delete(t->cli_fd);
1629 t->cli_state = CL_STCLOSE;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001630 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001631 if (!(t->flags & SN_ERR_MASK))
1632 t->flags |= SN_ERR_CLICL;
1633 if (!(t->flags & SN_FINST_MASK))
1634 t->flags |= SN_FINST_R;
1635 return 1;
1636 }
1637
1638 /* 3: has the read timeout expired ? */
Willy Tarreau036fae02008-01-06 13:24:40 +01001639 else if (unlikely(tv_isle(&req->rex, &now) ||
1640 tv_isle(&txn->exp, &now))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001641 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001642 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001643 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001644 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001645 if (!(t->flags & SN_ERR_MASK))
1646 t->flags |= SN_ERR_CLITO;
1647 if (!(t->flags & SN_FINST_MASK))
1648 t->flags |= SN_FINST_R;
1649 return 1;
1650 }
1651
1652 /* 4: do we need to re-enable the read socket ? */
Willy Tarreau66319382007-04-08 17:17:37 +02001653 else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001654 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreau58f10d72006-12-04 02:26:12 +01001655 * full. We cannot loop here since stream_sock_read will disable it only if
1656 * req->l == rlim-data
1657 */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01001658 if (!tv_add_ifset(&req->rex, &now, &t->fe->timeout.client))
Willy Tarreau58f10d72006-12-04 02:26:12 +01001659 tv_eternity(&req->rex);
1660 }
1661 return t->cli_state != CL_STHEADERS;
1662 }
1663
1664
1665 /****************************************************************
1666 * More interesting part now : we know that we have a complete *
1667 * request which at least looks like HTTP. We have an indicator *
1668 * of each header's length, so we can parse them quickly. *
1669 ****************************************************************/
1670
Willy Tarreau9cdde232007-05-02 20:58:19 +02001671 /* ensure we keep this pointer to the beginning of the message */
1672 msg->sol = req->data + msg->som;
1673
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 /*
1675 * 1: identify the method
1676 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001677 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001678
1679 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001680 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001681 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001682 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001683 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 if (unlikely((t->fe->monitor_uri_len != 0) &&
1685 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1686 !memcmp(&req->data[msg->sl.rq.u],
1687 t->fe->monitor_uri,
1688 t->fe->monitor_uri_len))) {
1689 /*
1690 * We have found the monitor URI
1691 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001692 struct acl_cond *cond;
1693 cur_proxy = t->fe;
1694
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001695 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001696
1697 /* Check if we want to fail this monitor request or not */
1698 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1699 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
1700 if (cond->pol == ACL_COND_UNLESS)
1701 ret = !ret;
1702
1703 if (ret) {
1704 /* we fail this request, let's return 503 service unavail */
1705 txn->status = 503;
1706 client_retnclose(t, error_message(t, HTTP_ERR_503));
1707 goto return_prx_cond;
1708 }
1709 }
1710
1711 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001712 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001713 client_retnclose(t, &http_200_chunk);
1714 goto return_prx_cond;
1715 }
1716
1717 /*
1718 * 3: Maybe we have to copy the original REQURI for the logs ?
1719 * Note: we cannot log anymore if the request has been
1720 * classified as invalid.
1721 */
1722 if (unlikely(t->logs.logwait & LW_REQ)) {
1723 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001724 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 int urilen = msg->sl.rq.l;
1726
1727 if (urilen >= REQURI_LEN)
1728 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001729 memcpy(txn->uri, &req->data[msg->som], urilen);
1730 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001731
1732 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001733 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 } else {
1735 Alert("HTTP logging : out of memory.\n");
1736 }
1737 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001738
Willy Tarreau06619262006-12-17 08:37:22 +01001739
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1741 if (unlikely(msg->sl.rq.v_l == 0)) {
1742 int delta;
1743 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001744 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 cur_end = msg->sol + msg->sl.rq.l;
1746 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001747
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 if (msg->sl.rq.u_l == 0) {
1749 /* if no URI was set, add "/" */
1750 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1751 cur_end += delta;
1752 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001753 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 /* add HTTP version */
1755 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1756 msg->eoh += delta;
1757 cur_end += delta;
1758 cur_end = (char *)http_parse_reqline(msg, req->data,
1759 HTTP_MSG_RQMETH,
1760 msg->sol, cur_end + 1,
1761 NULL, NULL);
1762 if (unlikely(!cur_end))
1763 goto return_bad_req;
1764
1765 /* we have a full HTTP/1.0 request now and we know that
1766 * we have either a CR or an LF at <ptr>.
1767 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001768 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001769 }
1770
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001771
1772 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001773 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001774 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001775 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001776
1777 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001779 * As opposed to version 1.2, now they will be evaluated in the
1780 * filters order and not in the header order. This means that
1781 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001782 *
1783 * We can now check whether we want to switch to another
1784 * backend, in which case we will re-check the backend's
1785 * filters and various options. In order to support 3-level
1786 * switching, here's how we should proceed :
1787 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001788 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001789 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001790 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001791 * There cannot be any switch from there, so ->be cannot be
1792 * changed anymore.
1793 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001794 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001795 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001796 * The response path will be able to apply either ->be, or
1797 * ->be then ->fe filters in order to match the reverse of
1798 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001799 */
1800
Willy Tarreau06619262006-12-17 08:37:22 +01001801 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001802 struct acl_cond *cond;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001803 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001804 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001805
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001806 /* first check whether we have some ACLs set to block this request */
1807 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001808 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001809 if (cond->pol == ACL_COND_UNLESS)
1810 ret = !ret;
1811
1812 if (ret) {
1813 txn->status = 403;
1814 /* let's log the request time */
1815 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
1816 client_retnclose(t, error_message(t, HTTP_ERR_403));
1817 goto return_prx_cond;
1818 }
1819 }
1820
Willy Tarreau06619262006-12-17 08:37:22 +01001821 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01001822 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001823 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
1824 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001825 }
1826
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001827 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
1828 /* to ensure correct connection accounting on
1829 * the backend, we count the connection for the
1830 * one managing the queue.
1831 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001832 t->be->beconn++;
1833 if (t->be->beconn > t->be->beconn_max)
1834 t->be->beconn_max = t->be->beconn;
1835 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001836 t->flags |= SN_BE_ASSIGNED;
1837 }
1838
Willy Tarreau06619262006-12-17 08:37:22 +01001839 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01001840 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01001841 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001842 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001843 /* let's log the request time */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001844 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreau80587432006-12-24 17:47:20 +01001845 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001846 goto return_prx_cond;
1847 }
1848
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001849 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01001850 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001851 !(t->flags & SN_CONN_CLOSED)) {
1852 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001853 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001854 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01001855
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001856 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001857 old_idx = 0;
1858
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001859 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1860 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001861 cur_ptr = cur_next;
1862 cur_end = cur_ptr + cur_hdr->len;
1863 cur_next = cur_end + cur_hdr->cr + 1;
1864
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001865 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1866 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 /* 3 possibilities :
1868 * - we have already set Connection: close,
1869 * so we remove this line.
1870 * - we have not yet set Connection: close,
1871 * but this line indicates close. We leave
1872 * it untouched and set the flag.
1873 * - we have not yet set Connection: close,
1874 * and this line indicates non-close. We
1875 * replace it.
1876 */
1877 if (t->flags & SN_CONN_CLOSED) {
1878 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001879 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001880 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001881 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1882 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 cur_hdr->len = 0;
1884 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001885 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1886 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1887 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001888 cur_next += delta;
1889 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001890 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001891 }
1892 t->flags |= SN_CONN_CLOSED;
1893 }
1894 }
1895 old_idx = cur_idx;
1896 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02001897 }
1898 /* add request headers from the rule sets in the same order */
1899 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
1900 if (unlikely(http_header_add_tail(req,
1901 &txn->req,
1902 &txn->hdr_idx,
1903 rule_set->req_add[cur_idx])) < 0)
1904 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01001905 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001906
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001907 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01001908 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001909 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01001910 /* we have to check the URI and auth for this request */
1911 if (stats_check_uri_auth(t, rule_set))
1912 return 1;
1913 }
1914
Willy Tarreau55ea7572007-06-17 19:56:27 +02001915 /* now check whether we have some switching rules for this request */
1916 if (!(t->flags & SN_BE_ASSIGNED)) {
1917 struct switching_rule *rule;
1918
1919 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
1920 int ret;
1921
1922 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
1923 if (cond->pol == ACL_COND_UNLESS)
1924 ret = !ret;
1925
1926 if (ret) {
1927 t->be = rule->be.backend;
1928 t->be->beconn++;
1929 if (t->be->beconn > t->be->beconn_max)
1930 t->be->beconn_max = t->be->beconn;
1931 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001932
1933 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01001934 t->rep->rto = t->req->wto = t->be->timeout.server;
1935 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001936 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02001937 t->flags |= SN_BE_ASSIGNED;
1938 break;
1939 }
1940 }
1941 }
1942
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001943 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
1944 /* No backend was set, but there was a default
1945 * backend set in the frontend, so we use it and
1946 * loop again.
1947 */
1948 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001949 t->be->beconn++;
1950 if (t->be->beconn > t->be->beconn_max)
1951 t->be->beconn_max = t->be->beconn;
1952 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001953
1954 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01001955 t->rep->rto = t->req->wto = t->be->timeout.server;
1956 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001957 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001958 t->flags |= SN_BE_ASSIGNED;
1959 }
1960 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01001961
Willy Tarreau58f10d72006-12-04 02:26:12 +01001962
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001963 if (!(t->flags & SN_BE_ASSIGNED)) {
1964 /* To ensure correct connection accounting on
1965 * the backend, we count the connection for the
1966 * one managing the queue.
1967 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001968 t->be->beconn++;
1969 if (t->be->beconn > t->be->beconn_max)
1970 t->be->beconn_max = t->be->beconn;
1971 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001972 t->flags |= SN_BE_ASSIGNED;
1973 }
1974
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001975 /*
1976 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01001977 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001978 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01001979 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01001980 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001981
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001982 /*
1983 * If HTTP PROXY is set we simply get remote server address
1984 * parsing incoming request.
1985 */
1986 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
1987 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
1988 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001989
Willy Tarreau2a324282006-12-05 00:05:46 +01001990 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001991 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01001992 * so let's do the same now.
1993 */
1994
1995 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001996 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001997 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01001998 }
1999
2000
2001 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002002 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002003 * Note that doing so might move headers in the request, but
2004 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002005 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002006 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002007 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2008 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002009 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002010
Willy Tarreau58f10d72006-12-04 02:26:12 +01002011
Willy Tarreau2a324282006-12-05 00:05:46 +01002012 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002013 * 9: add X-Forwarded-For if either the frontend or the backend
2014 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002015 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002016 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002017 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002018 /* Add an X-Forwarded-For header unless the source IP is
2019 * in the 'except' network range.
2020 */
2021 if ((!t->fe->except_mask.s_addr ||
2022 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2023 != t->fe->except_net.s_addr) &&
2024 (!t->be->except_mask.s_addr ||
2025 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2026 != t->be->except_net.s_addr)) {
2027 int len;
2028 unsigned char *pn;
2029 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002030
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002031 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d",
2032 pn[0], pn[1], pn[2], pn[3]);
2033
2034 if (unlikely(http_header_add_tail2(req, &txn->req,
2035 &txn->hdr_idx, trash, len)) < 0)
2036 goto return_bad_req;
2037 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002038 }
2039 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002040 /* FIXME: for the sake of completeness, we should also support
2041 * 'except' here, although it is mostly useless in this case.
2042 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002043 int len;
2044 char pn[INET6_ADDRSTRLEN];
2045 inet_ntop(AF_INET6,
2046 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2047 pn, sizeof(pn));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002048 len = sprintf(trash, "X-Forwarded-For: %s", pn);
2049 if (unlikely(http_header_add_tail2(req, &txn->req,
2050 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002051 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002052 }
2053 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002054
Willy Tarreau2a324282006-12-05 00:05:46 +01002055 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002056 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002057 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002058 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002059 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002060 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002061 if ((unlikely(msg->sl.rq.v_l != 8) ||
2062 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2063 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002064 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002065 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002066 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002067 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002068
Willy Tarreau2a324282006-12-05 00:05:46 +01002069 /*************************************************************
2070 * OK, that's finished for the headers. We have done what we *
2071 * could. Let's switch to the DATA state. *
2072 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002073
Willy Tarreau2a324282006-12-05 00:05:46 +01002074 t->cli_state = CL_STDATA;
2075 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002076
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002077 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002078
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002079 if (!tv_isset(&t->fe->timeout.client) ||
2080 (t->srv_state < SV_STDATA && tv_isset(&t->be->timeout.server))) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002081 /* If the client has no timeout, or if the server is not ready yet,
2082 * and we know for sure that it can expire, then it's cleaner to
2083 * disable the timeout on the client side so that too low values
2084 * cannot make the sessions abort too early.
2085 *
2086 * FIXME-20050705: the server needs a way to re-enable this time-out
2087 * when it switches its state, otherwise a client can stay connected
2088 * indefinitely. This now seems to be OK.
2089 */
2090 tv_eternity(&req->rex);
2091 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002092
Willy Tarreau1fa31262007-12-03 00:36:16 +01002093 /* When a connection is tarpitted, we use the tarpit timeout,
2094 * which may be the same as the connect timeout if unspecified.
2095 * If unset, then set it to zero because we really want it to
2096 * eventually expire.
Willy Tarreau2a324282006-12-05 00:05:46 +01002097 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002098 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002099 t->req->l = 0;
2100 /* flush the request so that we can drop the connection early
2101 * if the client closes first.
2102 */
Willy Tarreau1fa31262007-12-03 00:36:16 +01002103 if (!tv_add_ifset(&req->cex, &now, &t->be->timeout.tarpit))
Willy Tarreaud825eef2007-05-12 22:35:00 +02002104 req->cex = now;
Willy Tarreau2a324282006-12-05 00:05:46 +01002105 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002106
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002107 /* OK let's go on with the BODY now */
Willy Tarreau06619262006-12-17 08:37:22 +01002108 goto process_data;
2109
2110 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002111 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002112 txn->status = 400;
Willy Tarreau80587432006-12-24 17:47:20 +01002113 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002114 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002115 return_prx_cond:
2116 if (!(t->flags & SN_ERR_MASK))
2117 t->flags |= SN_ERR_PRXCOND;
2118 if (!(t->flags & SN_FINST_MASK))
2119 t->flags |= SN_FINST_R;
2120 return 1;
2121
Willy Tarreaubaaee002006-06-26 02:48:02 +02002122 }
2123 else if (c == CL_STDATA) {
2124 process_data:
2125 /* FIXME: this error handling is partly buggy because we always report
2126 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
2127 * or HEADER phase. BTW, it's not logical to expire the client while
2128 * we're waiting for the server to connect.
2129 */
2130 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002131 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002132 buffer_shutr(req);
2133 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002134 fd_delete(t->cli_fd);
2135 t->cli_state = CL_STCLOSE;
2136 if (!(t->flags & SN_ERR_MASK))
2137 t->flags |= SN_ERR_CLICL;
2138 if (!(t->flags & SN_FINST_MASK)) {
2139 if (t->pend_pos)
2140 t->flags |= SN_FINST_Q;
2141 else if (s == SV_STCONN)
2142 t->flags |= SN_FINST_C;
2143 else
2144 t->flags |= SN_FINST_D;
2145 }
2146 return 1;
2147 }
2148 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002149 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002150 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002151 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002152 t->cli_state = CL_STSHUTR;
2153 return 1;
2154 }
2155 /* last server read and buffer empty */
2156 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002157 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002158 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002159 shutdown(t->cli_fd, SHUT_WR);
2160 /* We must ensure that the read part is still alive when switching
2161 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002162 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002163 tv_add_ifset(&req->rex, &now, &t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002164 t->cli_state = CL_STSHUTW;
2165 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2166 return 1;
2167 }
2168 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002169 else if (tv_isle(&req->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002170 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002171 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002172 t->cli_state = CL_STSHUTR;
2173 if (!(t->flags & SN_ERR_MASK))
2174 t->flags |= SN_ERR_CLITO;
2175 if (!(t->flags & SN_FINST_MASK)) {
2176 if (t->pend_pos)
2177 t->flags |= SN_FINST_Q;
2178 else if (s == SV_STCONN)
2179 t->flags |= SN_FINST_C;
2180 else
2181 t->flags |= SN_FINST_D;
2182 }
2183 return 1;
2184 }
2185 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002186 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002187 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002188 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002189 shutdown(t->cli_fd, SHUT_WR);
2190 /* We must ensure that the read part is still alive when switching
2191 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002192 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002193 tv_add_ifset(&req->rex, &now, &t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002194
2195 t->cli_state = CL_STSHUTW;
2196 if (!(t->flags & SN_ERR_MASK))
2197 t->flags |= SN_ERR_CLITO;
2198 if (!(t->flags & SN_FINST_MASK)) {
2199 if (t->pend_pos)
2200 t->flags |= SN_FINST_Q;
2201 else if (s == SV_STCONN)
2202 t->flags |= SN_FINST_C;
2203 else
2204 t->flags |= SN_FINST_D;
2205 }
2206 return 1;
2207 }
2208
2209 if (req->l >= req->rlim - req->data) {
2210 /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02002211 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002212 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002213 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002214 }
2215 } else {
2216 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002217 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002218 if (!tv_isset(&t->fe->timeout.client) ||
2219 (t->srv_state < SV_STDATA && tv_isset(&t->be->timeout.server)))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002220 /* If the client has no timeout, or if the server not ready yet, and we
2221 * know for sure that it can expire, then it's cleaner to disable the
2222 * timeout on the client side so that too low values cannot make the
2223 * sessions abort too early.
2224 */
Willy Tarreaud7971282006-07-29 18:36:34 +02002225 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002226 else
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002227 tv_add(&req->rex, &now, &t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002228 }
2229 }
2230
2231 if ((rep->l == 0) ||
2232 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002233 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2234 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002235 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002236 }
2237 } else {
2238 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002239 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2240 /* restart writing */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002241 if (tv_add_ifset(&rep->wex, &now, &t->fe->timeout.client)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002242 /* FIXME: to prevent the client from expiring read timeouts during writes,
2243 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002244 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002245 }
2246 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002247 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002248 }
2249 }
2250 return 0; /* other cases change nothing */
2251 }
2252 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002253 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002254 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002255 fd_delete(t->cli_fd);
2256 t->cli_state = CL_STCLOSE;
2257 if (!(t->flags & SN_ERR_MASK))
2258 t->flags |= SN_ERR_CLICL;
2259 if (!(t->flags & SN_FINST_MASK)) {
2260 if (t->pend_pos)
2261 t->flags |= SN_FINST_Q;
2262 else if (s == SV_STCONN)
2263 t->flags |= SN_FINST_C;
2264 else
2265 t->flags |= SN_FINST_D;
2266 }
2267 return 1;
2268 }
2269 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
2270 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002271 buffer_shutw(rep);
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(&rep->wex, &now)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002277 buffer_shutw(rep);
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
2293 if (t->flags & SN_SELF_GEN) {
2294 produce_content(t);
2295 if (rep->l == 0) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002296 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002297 fd_delete(t->cli_fd);
2298 t->cli_state = CL_STCLOSE;
2299 return 1;
2300 }
2301 }
2302
2303 if ((rep->l == 0)
2304 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002305 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2306 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002307 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002308 }
2309 } else {
2310 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002311 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2312 /* restart writing */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002313 if (!tv_add_ifset(&rep->wex, &now, &t->fe->timeout.client))
Willy Tarreaud7971282006-07-29 18:36:34 +02002314 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002315 }
2316 }
2317 return 0;
2318 }
2319 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002320 if (req->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002321 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002322 fd_delete(t->cli_fd);
2323 t->cli_state = CL_STCLOSE;
2324 if (!(t->flags & SN_ERR_MASK))
2325 t->flags |= SN_ERR_CLICL;
2326 if (!(t->flags & SN_FINST_MASK)) {
2327 if (t->pend_pos)
2328 t->flags |= SN_FINST_Q;
2329 else if (s == SV_STCONN)
2330 t->flags |= SN_FINST_C;
2331 else
2332 t->flags |= SN_FINST_D;
2333 }
2334 return 1;
2335 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002336 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002337 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002338 fd_delete(t->cli_fd);
2339 t->cli_state = CL_STCLOSE;
2340 return 1;
2341 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002342 else if (tv_isle(&req->rex, &now)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002343 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002344 fd_delete(t->cli_fd);
2345 t->cli_state = CL_STCLOSE;
2346 if (!(t->flags & SN_ERR_MASK))
2347 t->flags |= SN_ERR_CLITO;
2348 if (!(t->flags & SN_FINST_MASK)) {
2349 if (t->pend_pos)
2350 t->flags |= SN_FINST_Q;
2351 else if (s == SV_STCONN)
2352 t->flags |= SN_FINST_C;
2353 else
2354 t->flags |= SN_FINST_D;
2355 }
2356 return 1;
2357 }
2358 else if (req->l >= req->rlim - req->data) {
2359 /* no room to read more data */
2360
2361 /* FIXME-20050705: is it possible for a client to maintain a session
2362 * after the timeout by sending more data after it receives a close ?
2363 */
2364
Willy Tarreau66319382007-04-08 17:17:37 +02002365 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002366 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002367 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002368 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2369 }
2370 } else {
2371 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002372 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002373 if (!tv_add_ifset(&req->rex, &now, &t->fe->timeout.client))
Willy Tarreaud7971282006-07-29 18:36:34 +02002374 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002375 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2376 }
2377 }
2378 return 0;
2379 }
2380 else { /* CL_STCLOSE: nothing to do */
2381 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2382 int len;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002383 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 +02002384 write(1, trash, len);
2385 }
2386 return 0;
2387 }
2388 return 0;
2389}
2390
2391
2392/*
2393 * manages the server FSM and its socket. It returns 1 if a state has changed
2394 * (and a resync may be needed), 0 else.
2395 */
2396int process_srv(struct session *t)
2397{
2398 int s = t->srv_state;
2399 int c = t->cli_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002400 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002401 struct buffer *req = t->req;
2402 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002403 int conn_err;
2404
2405#ifdef DEBUG_FULL
2406 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
2407#endif
Willy Tarreauee991362007-05-14 14:37:50 +02002408
2409#if 0
2410 fprintf(stderr,"%s:%d fe->clito=%d.%d, fe->conto=%d.%d, fe->srvto=%d.%d\n",
2411 __FUNCTION__, __LINE__,
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002412 t->fe->timeout.client.tv_sec, t->fe->timeout.client.tv_usec,
2413 t->fe->timeout.connect.tv_sec, t->fe->timeout.connect.tv_usec,
2414 t->fe->timeout.server.tv_sec, t->fe->timeout.server.tv_usec);
Willy Tarreauee991362007-05-14 14:37:50 +02002415 fprintf(stderr,"%s:%d be->clito=%d.%d, be->conto=%d.%d, be->srvto=%d.%d\n",
2416 __FUNCTION__, __LINE__,
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002417 t->be->timeout.client.tv_sec, t->be->timeout.client.tv_usec,
2418 t->be->timeout.connect.tv_sec, t->be->timeout.connect.tv_usec,
2419 t->be->timeout.server.tv_sec, t->be->timeout.server.tv_usec);
Willy Tarreauee991362007-05-14 14:37:50 +02002420
2421 fprintf(stderr,"%s:%d req->cto=%d.%d, req->rto=%d.%d, req->wto=%d.%d\n",
2422 __FUNCTION__, __LINE__,
2423 req->cto.tv_sec, req->cto.tv_usec,
2424 req->rto.tv_sec, req->rto.tv_usec,
2425 req->wto.tv_sec, req->wto.tv_usec);
2426
2427 fprintf(stderr,"%s:%d rep->cto=%d.%d, rep->rto=%d.%d, rep->wto=%d.%d\n",
2428 __FUNCTION__, __LINE__,
2429 rep->cto.tv_sec, rep->cto.tv_usec,
2430 rep->rto.tv_sec, rep->rto.tv_usec,
2431 rep->wto.tv_sec, rep->wto.tv_usec);
2432#endif
2433
Willy Tarreaubaaee002006-06-26 02:48:02 +02002434 //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 +02002435 //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
2436 //EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002437 //);
2438 if (s == SV_STIDLE) {
2439 if (c == CL_STHEADERS)
2440 return 0; /* stay in idle, waiting for data to reach the client side */
2441 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2442 (c == CL_STSHUTR &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002443 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002444 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002445 if (t->pend_pos)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002446 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002447 /* note that this must not return any error because it would be able to
2448 * overwrite the client_retnclose() output.
2449 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002450 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002451 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002452 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002453 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 +02002454
2455 return 1;
2456 }
2457 else {
Willy Tarreau3d300592007-03-18 18:34:41 +01002458 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002459 /* This connection is being tarpitted. The CLIENT side has
2460 * already set the connect expiration date to the right
2461 * timeout. We just have to check that it has not expired.
2462 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002463 if (!tv_isle(&req->cex, &now))
Willy Tarreaub8750a82006-09-03 09:56:00 +02002464 return 0;
2465
2466 /* We will set the queue timer to the time spent, just for
2467 * logging purposes. We fake a 500 server error, so that the
2468 * attacker will not suspect his connection has been tarpitted.
2469 * It will not cause trouble to the logs because we can exclude
2470 * the tarpitted connections by filtering on the 'PT' status flags.
2471 */
2472 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002473 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub8750a82006-09-03 09:56:00 +02002474 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002475 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002476 return 1;
2477 }
2478
Willy Tarreaubaaee002006-06-26 02:48:02 +02002479 /* Right now, we will need to create a connection to the server.
2480 * We might already have tried, and got a connection pending, in
2481 * which case we will not do anything till it's pending. It's up
2482 * to any other session to release it and wake us up again.
2483 */
2484 if (t->pend_pos) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002485 if (!tv_isle(&req->cex, &now))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002486 return 0;
2487 else {
2488 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002489 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002490 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002491 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002492 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002493 if (t->srv)
2494 t->srv->failed_conns++;
Willy Tarreau50fd1e12007-12-10 15:25:35 +01002495 t->be->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002496 return 1;
2497 }
2498 }
2499
2500 do {
2501 /* first, get a connection */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002502 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2503 t->flags |= SN_REDIRECTABLE;
2504
Willy Tarreaubaaee002006-06-26 02:48:02 +02002505 if (srv_redispatch_connect(t))
2506 return t->srv_state != SV_STIDLE;
2507
Willy Tarreau21d2af32008-02-14 20:25:24 +01002508 if ((t->flags & SN_REDIRECTABLE) && t->srv && t->srv->rdr_len) {
2509 /* Server supporting redirection and it is possible.
2510 * Invalid requests are reported as such. It concerns all
2511 * the largest ones.
2512 */
2513 struct chunk rdr;
2514 char *path;
2515 int len;
2516
2517 /* 1: create the response header */
2518 rdr.len = strlen(HTTP_302);
2519 rdr.str = trash;
2520 memcpy(rdr.str, HTTP_302, rdr.len);
2521
2522 /* 2: add the server's prefix */
2523 if (rdr.len + t->srv->rdr_len > sizeof(trash))
2524 goto cancel_redir;
2525
2526 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
2527 rdr.len += t->srv->rdr_len;
2528
2529 /* 3: add the request URI */
2530 path = http_get_path(txn);
2531 if (!path)
2532 goto cancel_redir;
2533 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2534 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
2535 goto cancel_redir;
2536
2537 memcpy(rdr.str + rdr.len, path, len);
2538 rdr.len += len;
2539 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2540 rdr.len += 4;
2541
2542 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
2543 /* FIXME: we should increase a counter of redirects per server and per backend. */
2544 if (t->srv)
2545 t->srv->cum_sess++;
2546 return 1;
2547 cancel_redir:
2548 txn->status = 400;
2549 t->fe->failed_req++;
2550 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
2551 400, error_message(t, HTTP_ERR_400));
2552 return 1;
2553 }
2554
Willy Tarreaubaaee002006-06-26 02:48:02 +02002555 /* try to (re-)connect to the server, and fail if we expire the
2556 * number of retries.
2557 */
2558 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002559 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002560 return t->srv_state != SV_STIDLE;
2561 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002562 } while (1);
2563 }
2564 }
2565 else if (s == SV_STCONN) { /* connection in progress */
2566 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2567 (c == CL_STSHUTR &&
Willy Tarreauc9b654b2007-05-08 14:46:53 +02002568 ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
2569 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002570 tv_eternity(&req->cex);
Willy Tarreauf899b942008-03-28 18:09:38 +01002571 if (!(t->flags & SN_CONN_TAR)) {
2572 /* if we are in turn-around, we have already closed the FD */
2573 fd_delete(t->srv_fd);
2574 if (t->srv) {
2575 t->srv->cur_sess--;
2576 if (t->srv->proxy->lbprm.server_drop_conn)
2577 t->srv->proxy->lbprm.server_drop_conn(t->srv);
2578 }
Willy Tarreau51406232008-03-10 22:04:20 +01002579 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002580
2581 /* note that this must not return any error because it would be able to
2582 * overwrite the client_retnclose() output.
2583 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002584 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002585 return 1;
2586 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002587 if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002588 //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 +02002589 return 0; /* nothing changed */
2590 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002591 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002592 /* timeout, asynchronous connect error or first write error */
2593 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2594
Willy Tarreau541b5c22008-01-06 23:34:21 +01002595 if (t->flags & SN_CONN_TAR) {
2596 /* We are doing a turn-around waiting for a new connection attempt. */
2597 if (!tv_isle(&req->cex, &now))
2598 return 0;
2599 t->flags &= ~SN_CONN_TAR;
2600 }
2601 else {
2602 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01002603 if (t->srv) {
Willy Tarreau541b5c22008-01-06 23:34:21 +01002604 t->srv->cur_sess--;
Willy Tarreau51406232008-03-10 22:04:20 +01002605 if (t->srv->proxy->lbprm.server_drop_conn)
2606 t->srv->proxy->lbprm.server_drop_conn(t->srv);
2607 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002608
Willy Tarreau541b5c22008-01-06 23:34:21 +01002609 if (!(req->flags & BF_WRITE_STATUS))
2610 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2611 else
2612 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2613
2614 /* ensure that we have enough retries left */
2615 if (srv_count_retry_down(t, conn_err))
2616 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002617
Willy Tarreau541b5c22008-01-06 23:34:21 +01002618 if (req->flags & BF_WRITE_ERROR) {
2619 /* we encountered an immediate connection error, and we
2620 * will have to retry connecting to the same server, most
2621 * likely leading to the same result. To avoid this, we
2622 * fake a connection timeout to retry after a turn-around
2623 * time of 1 second. We will wait in the previous if block.
2624 */
2625 t->flags |= SN_CONN_TAR;
2626 tv_ms_add(&req->cex, &now, 1000);
2627 return 0;
2628 }
2629 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002630
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002631 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002632 /* We're on our last chance, and the REDISP option was specified.
2633 * We will ignore cookie and force to balance or use the dispatcher.
2634 */
2635 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002636 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002637 task_wakeup(t->srv->queue_mgt);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002638
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01002639 /* it's left to the dispatcher to choose a server */
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002640 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002641
2642 /* first, get a connection */
2643 if (srv_redispatch_connect(t))
Willy Tarreau00559e72008-01-06 23:46:19 +01002644 return t->srv_state != SV_STCONN;
Krzysztof Piotr Oledzki626a19b2008-02-04 02:10:09 +01002645 } else {
2646 if (t->srv)
2647 t->srv->retries++;
2648 t->be->retries++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002649 }
2650
Willy Tarreaubaaee002006-06-26 02:48:02 +02002651 do {
2652 /* Now we will try to either reconnect to the same server or
2653 * connect to another server. If the connection gets queued
2654 * because all servers are saturated, then we will go back to
2655 * the SV_STIDLE state.
2656 */
2657 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002658 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002659 return t->srv_state != SV_STCONN;
2660 }
2661
2662 /* we need to redispatch the connection to another server */
2663 if (srv_redispatch_connect(t))
2664 return t->srv_state != SV_STCONN;
2665 } while (1);
2666 }
2667 else { /* no error or write 0 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002668 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002669
2670 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2671 if (req->l == 0) /* nothing to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002672 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002673 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002674 } else /* need the right to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002675 EV_FD_SET(t->srv_fd, DIR_WR);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002676 if (tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002677 /* FIXME: to prevent the server from expiring read timeouts during writes,
2678 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002679 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002680 }
2681 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002682 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002683 }
2684
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002685 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreauf161a342007-04-08 16:59:42 +02002686 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002687 if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server))
Willy Tarreaud7971282006-07-29 18:36:34 +02002688 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002689
2690 t->srv_state = SV_STDATA;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002691 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2692
2693 /* if the user wants to log as soon as possible, without counting
2694 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002695 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002696 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
Willy Tarreau42250582007-04-01 01:30:43 +02002697 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002698 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002699#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002700 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002701 /* TCP splicing supported by both FE and BE */
2702 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2703 }
2704#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002705 }
2706 else {
2707 t->srv_state = SV_STHEADERS;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002708 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002709 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2710 /* reset hdr_idx which was already initialized by the request.
2711 * right now, the http parser does it.
2712 * hdr_idx_init(&t->txn.hdr_idx);
2713 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002714 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002715 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002716 return 1;
2717 }
2718 }
2719 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002720 /*
2721 * Now parse the partial (or complete) lines.
2722 * We will check the response syntax, and also join multi-line
2723 * headers. An index of all the lines will be elaborated while
2724 * parsing.
2725 *
2726 * For the parsing, we use a 28 states FSM.
2727 *
2728 * Here is the information we currently have :
2729 * rep->data + req->som = beginning of response
2730 * rep->data + req->eoh = end of processed headers / start of current one
2731 * rep->data + req->eol = end of current header or line (LF or CRLF)
2732 * rep->lr = first non-visited byte
2733 * rep->r = end of data
2734 */
2735
2736 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002737 struct http_msg *msg = &txn->rsp;
2738 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002739
Willy Tarreaua15645d2007-03-18 16:22:39 +01002740 if (likely(rep->lr < rep->r))
2741 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002742
Willy Tarreaua15645d2007-03-18 16:22:39 +01002743 /* 1: we might have to print this header in debug mode */
2744 if (unlikely((global.mode & MODE_DEBUG) &&
2745 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2746 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2747 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002748
Willy Tarreaua15645d2007-03-18 16:22:39 +01002749 sol = rep->data + msg->som;
2750 eol = sol + msg->sl.rq.l;
2751 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002752
Willy Tarreaua15645d2007-03-18 16:22:39 +01002753 sol += hdr_idx_first_pos(&txn->hdr_idx);
2754 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002755
Willy Tarreaua15645d2007-03-18 16:22:39 +01002756 while (cur_idx) {
2757 eol = sol + txn->hdr_idx.v[cur_idx].len;
2758 debug_hdr("srvhdr", t, sol, eol);
2759 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2760 cur_idx = txn->hdr_idx.v[cur_idx].next;
2761 }
2762 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002763
Willy Tarreaubaaee002006-06-26 02:48:02 +02002764
Willy Tarreau66319382007-04-08 17:17:37 +02002765 if ((rep->l < rep->rlim - rep->data) && EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002766 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreaua15645d2007-03-18 16:22:39 +01002767 * full. We cannot loop here since stream_sock_read will disable it only if
2768 * rep->l == rlim-data
2769 */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002770 if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002771 tv_eternity(&rep->rex);
2772 }
2773
2774
2775 /*
2776 * Now we quickly check if we have found a full valid response.
2777 * If not so, we check the FD and buffer states before leaving.
2778 * A full response is indicated by the fact that we have seen
2779 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2780 * responses are checked first.
2781 *
2782 * Depending on whether the client is still there or not, we
2783 * may send an error response back or not. Note that normally
2784 * we should only check for HTTP status there, and check I/O
2785 * errors somewhere else.
2786 */
2787
2788 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2789
2790 /* Invalid response, or read error or write error */
2791 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2792 (req->flags & BF_WRITE_ERROR) ||
2793 (rep->flags & BF_READ_ERROR))) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002794 buffer_shutr(rep);
2795 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002796 fd_delete(t->srv_fd);
2797 if (t->srv) {
2798 t->srv->cur_sess--;
2799 t->srv->failed_resp++;
Willy Tarreau51406232008-03-10 22:04:20 +01002800 if (t->srv->proxy->lbprm.server_drop_conn)
2801 t->srv->proxy->lbprm.server_drop_conn(t->srv);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002802 }
2803 t->be->failed_resp++;
2804 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002805 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002806 client_return(t, error_message(t, HTTP_ERR_502));
2807 if (!(t->flags & SN_ERR_MASK))
2808 t->flags |= SN_ERR_SRVCL;
2809 if (!(t->flags & SN_FINST_MASK))
2810 t->flags |= SN_FINST_H;
2811 /* We used to have a free connection slot. Since we'll never use it,
2812 * we have to inform the server that it may be used by another session.
2813 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002814 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002815 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002816
Willy Tarreaua15645d2007-03-18 16:22:39 +01002817 return 1;
2818 }
2819
2820 /* end of client write or end of server read.
2821 * since we are in header mode, if there's no space left for headers, we
2822 * won't be able to free more later, so the session will never terminate.
2823 */
2824 else if (unlikely(rep->flags & BF_READ_NULL ||
2825 c == CL_STSHUTW || c == CL_STCLOSE ||
2826 rep->l >= rep->rlim - rep->data)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002827 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002828 buffer_shutr(rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002829 t->srv_state = SV_STSHUTR;
2830 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2831 return 1;
2832 }
2833
2834 /* read timeout : return a 504 to the client.
2835 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002836 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002837 tv_isle(&rep->rex, &now))) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002838 buffer_shutr(rep);
2839 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002840 fd_delete(t->srv_fd);
2841 if (t->srv) {
2842 t->srv->cur_sess--;
2843 t->srv->failed_resp++;
Willy Tarreau51406232008-03-10 22:04:20 +01002844 if (t->srv->proxy->lbprm.server_drop_conn)
2845 t->srv->proxy->lbprm.server_drop_conn(t->srv);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002846 }
2847 t->be->failed_resp++;
2848 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002849 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002850 client_return(t, error_message(t, HTTP_ERR_504));
2851 if (!(t->flags & SN_ERR_MASK))
2852 t->flags |= SN_ERR_SRVTO;
2853 if (!(t->flags & SN_FINST_MASK))
2854 t->flags |= SN_FINST_H;
2855 /* We used to have a free connection slot. Since we'll never use it,
2856 * we have to inform the server that it may be used by another session.
2857 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002858 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002859 task_wakeup(t->srv->queue_mgt);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002860 return 1;
2861 }
2862
2863 /* last client read and buffer empty */
2864 /* FIXME!!! here, we don't want to switch to SHUTW if the
2865 * client shuts read too early, because we may still have
2866 * some work to do on the headers.
2867 * The side-effect is that if the client completely closes its
2868 * connection during SV_STHEADER, the connection to the server
2869 * is kept until a response comes back or the timeout is reached.
2870 */
2871 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2872 (req->l == 0))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002873 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002874 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002875
2876 /* We must ensure that the read part is still
2877 * alive when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002878 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002879 tv_add_ifset(&rep->rex, &now, &t->be->timeout.server);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002880
2881 shutdown(t->srv_fd, SHUT_WR);
2882 t->srv_state = SV_STSHUTW;
2883 return 1;
2884 }
2885
2886 /* write timeout */
2887 /* FIXME!!! here, we don't want to switch to SHUTW if the
2888 * client shuts read too early, because we may still have
2889 * some work to do on the headers.
2890 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002891 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002892 tv_isle(&req->wex, &now))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002893 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002894 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002895 shutdown(t->srv_fd, SHUT_WR);
2896 /* We must ensure that the read part is still alive
2897 * when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002898 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002899 tv_add_ifset(&rep->rex, &now, &t->be->timeout.server);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002900
2901 t->srv_state = SV_STSHUTW;
2902 if (!(t->flags & SN_ERR_MASK))
2903 t->flags |= SN_ERR_SRVTO;
2904 if (!(t->flags & SN_FINST_MASK))
2905 t->flags |= SN_FINST_H;
2906 return 1;
2907 }
2908
2909 /*
2910 * And now the non-error cases.
2911 */
2912
2913 /* Data remaining in the request buffer.
2914 * This happens during the first pass here, and during
2915 * long posts.
2916 */
2917 else if (likely(req->l)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002918 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
2919 /* restart writing */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002920 if (tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002921 /* FIXME: to prevent the server from expiring read timeouts during writes,
2922 * we refresh it. */
2923 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002924 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002925 else
2926 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002927 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002928 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002929
Willy Tarreaua15645d2007-03-18 16:22:39 +01002930 /* nothing left in the request buffer */
2931 else {
Willy Tarreau66319382007-04-08 17:17:37 +02002932 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
2933 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002934 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002935 }
2936 }
2937
2938 return t->srv_state != SV_STHEADERS;
2939 }
2940
2941
2942 /*****************************************************************
2943 * More interesting part now : we know that we have a complete *
2944 * response which at least looks like HTTP. We have an indicator *
2945 * of each header's length, so we can parse them quickly. *
2946 ****************************************************************/
2947
Willy Tarreau9cdde232007-05-02 20:58:19 +02002948 /* ensure we keep this pointer to the beginning of the message */
2949 msg->sol = rep->data + msg->som;
2950
Willy Tarreaua15645d2007-03-18 16:22:39 +01002951 /*
2952 * 1: get the status code and check for cacheability.
2953 */
2954
2955 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002956 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002957
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002958 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002959 case 200:
2960 case 203:
2961 case 206:
2962 case 300:
2963 case 301:
2964 case 410:
2965 /* RFC2616 @13.4:
2966 * "A response received with a status code of
2967 * 200, 203, 206, 300, 301 or 410 MAY be stored
2968 * by a cache (...) unless a cache-control
2969 * directive prohibits caching."
2970 *
2971 * RFC2616 @9.5: POST method :
2972 * "Responses to this method are not cacheable,
2973 * unless the response includes appropriate
2974 * Cache-Control or Expires header fields."
2975 */
2976 if (likely(txn->meth != HTTP_METH_POST) &&
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002977 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
Willy Tarreau3d300592007-03-18 18:34:41 +01002978 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002979 break;
2980 default:
2981 break;
2982 }
2983
2984 /*
2985 * 2: we may need to capture headers
2986 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002987 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002988 capture_headers(rep->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002989 txn->rsp.cap, t->fe->rsp_cap);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002990
2991 /*
2992 * 3: we will have to evaluate the filters.
2993 * As opposed to version 1.2, now they will be evaluated in the
2994 * filters order and not in the header order. This means that
2995 * each filter has to be validated among all headers.
2996 *
2997 * Filters are tried with ->be first, then with ->fe if it is
2998 * different from ->be.
2999 */
3000
3001 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
3002
3003 cur_proxy = t->be;
3004 while (1) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003005 struct proxy *rule_set = cur_proxy;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003006
3007 /* try headers filters */
3008 if (rule_set->rsp_exp != NULL) {
3009 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
3010 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003011 if (t->srv) {
3012 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003013 t->srv->failed_resp++;
Willy Tarreau51406232008-03-10 22:04:20 +01003014 if (t->srv->proxy->lbprm.server_drop_conn)
3015 t->srv->proxy->lbprm.server_drop_conn(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003016 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003017 cur_proxy->failed_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003018 return_srv_prx_502:
Willy Tarreaufa645582007-06-03 15:59:52 +02003019 buffer_shutr(rep);
3020 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003021 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003022 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003023 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01003024 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02003025 if (!(t->flags & SN_ERR_MASK))
3026 t->flags |= SN_ERR_PRXCOND;
3027 if (!(t->flags & SN_FINST_MASK))
3028 t->flags |= SN_FINST_H;
3029 /* We used to have a free connection slot. Since we'll never use it,
3030 * we have to inform the server that it may be used by another session.
3031 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003032 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003033 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003034 return 1;
3035 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003036 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003037
Willy Tarreaua15645d2007-03-18 16:22:39 +01003038 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01003039 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003040 if (t->srv) {
3041 t->srv->cur_sess--;
3042 t->srv->failed_secu++;
Willy Tarreau51406232008-03-10 22:04:20 +01003043 if (t->srv->proxy->lbprm.server_drop_conn)
3044 t->srv->proxy->lbprm.server_drop_conn(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003045 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003046 cur_proxy->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003047 goto return_srv_prx_502;
3048 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003049
Willy Tarreaua15645d2007-03-18 16:22:39 +01003050 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003051 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003052 !(t->flags & SN_CONN_CLOSED)) {
3053 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003054 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003055 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003056
Willy Tarreaua15645d2007-03-18 16:22:39 +01003057 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3058 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003059
Willy Tarreaua15645d2007-03-18 16:22:39 +01003060 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3061 cur_hdr = &txn->hdr_idx.v[cur_idx];
3062 cur_ptr = cur_next;
3063 cur_end = cur_ptr + cur_hdr->len;
3064 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003065
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003066 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3067 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003068 /* 3 possibilities :
3069 * - we have already set Connection: close,
3070 * so we remove this line.
3071 * - we have not yet set Connection: close,
3072 * but this line indicates close. We leave
3073 * it untouched and set the flag.
3074 * - we have not yet set Connection: close,
3075 * and this line indicates non-close. We
3076 * replace it.
3077 */
3078 if (t->flags & SN_CONN_CLOSED) {
3079 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3080 txn->rsp.eoh += delta;
3081 cur_next += delta;
3082 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3083 txn->hdr_idx.used--;
3084 cur_hdr->len = 0;
3085 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003086 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
3087 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3088 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003089 cur_next += delta;
3090 cur_hdr->len += delta;
3091 txn->rsp.eoh += delta;
3092 }
3093 t->flags |= SN_CONN_CLOSED;
3094 }
3095 }
3096 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003097 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003098 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003099
Willy Tarreaua15645d2007-03-18 16:22:39 +01003100 /* add response headers from the rule sets in the same order */
3101 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003102 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3103 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003104 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003105 }
3106
Willy Tarreaua15645d2007-03-18 16:22:39 +01003107 /* check whether we're already working on the frontend */
3108 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003109 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003110 cur_proxy = t->fe;
3111 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003112
Willy Tarreaua15645d2007-03-18 16:22:39 +01003113 /*
3114 * 4: check for server cookie.
3115 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01003116 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
3117 || (t->be->options & PR_O_CHK_CACHE))
3118 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003119
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003120
Willy Tarreaua15645d2007-03-18 16:22:39 +01003121 /*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003122 * 5: check for cache-control or pragma headers if required.
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003123 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01003124 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
3125 check_response_for_cacheability(t, rep);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003126
3127 /*
3128 * 6: add server cookie in the response if needed
Willy Tarreaua15645d2007-03-18 16:22:39 +01003129 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003130 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3131 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003132 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003133
Willy Tarreaua15645d2007-03-18 16:22:39 +01003134 /* the server is known, it's not the one the client requested, we have to
3135 * insert a set-cookie here, except if we want to insert only on POST
3136 * requests and this one isn't. Note that servers which don't have cookies
3137 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003138 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003139 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003140 t->be->cookie_name,
Willy Tarreaua15645d2007-03-18 16:22:39 +01003141 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003142
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003143 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3144 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003145 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01003146 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003147
Willy Tarreaua15645d2007-03-18 16:22:39 +01003148 /* Here, we will tell an eventual cache on the client side that we don't
3149 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3150 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3151 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3152 */
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003153 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
3154
3155 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3156
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003157 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3158 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003159 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003160 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003161 }
3162
3163
3164 /*
Willy Tarreaua15645d2007-03-18 16:22:39 +01003165 * 7: check if result will be cacheable with a cookie.
3166 * We'll block the response if security checks have caught
3167 * nasty things such as a cacheable cookie.
3168 */
Willy Tarreau3d300592007-03-18 18:34:41 +01003169 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3170 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003171 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003172
Willy Tarreaua15645d2007-03-18 16:22:39 +01003173 /* we're in presence of a cacheable response containing
3174 * a set-cookie header. We'll block it as requested by
3175 * the 'checkcache' option, and send an alert.
3176 */
3177 if (t->srv) {
3178 t->srv->cur_sess--;
3179 t->srv->failed_secu++;
Willy Tarreau51406232008-03-10 22:04:20 +01003180 if (t->srv->proxy->lbprm.server_drop_conn)
3181 t->srv->proxy->lbprm.server_drop_conn(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003182 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003183 t->be->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003184
Willy Tarreaua15645d2007-03-18 16:22:39 +01003185 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003186 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003187 send_log(t->be, LOG_ALERT,
3188 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003189 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003190 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003191 }
3192
Willy Tarreaua15645d2007-03-18 16:22:39 +01003193 /*
3194 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02003195 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003196 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02003197 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003198 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02003199 if ((unlikely(msg->sl.st.v_l != 8) ||
3200 unlikely(req->data[msg->som + 7] != '0')) &&
3201 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003202 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003203 goto return_bad_resp;
3204 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003205 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003206
Willy Tarreaubaaee002006-06-26 02:48:02 +02003207
Willy Tarreaua15645d2007-03-18 16:22:39 +01003208 /*************************************************************
3209 * OK, that's finished for the headers. We have done what we *
3210 * could. Let's switch to the DATA state. *
3211 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003212
Willy Tarreaua15645d2007-03-18 16:22:39 +01003213 t->srv_state = SV_STDATA;
3214 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003215 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003216
3217 /* client connection already closed or option 'forceclose' required :
3218 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003219 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003220 if ((req->l == 0) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003221 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003222 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003223 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003224
3225 /* We must ensure that the read part is still alive when switching
3226 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003227 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003228 tv_add_ifset(&rep->rex, &now, &t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003229
Willy Tarreaua15645d2007-03-18 16:22:39 +01003230 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003231 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003232 }
3233
Willy Tarreaua15645d2007-03-18 16:22:39 +01003234#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003235 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003236 /* TCP splicing supported by both FE and BE */
3237 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003238 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003239#endif
3240 /* if the user wants to log as soon as possible, without counting
Krzysztof Piotr Oledzkif1e1cb42008-01-20 23:27:02 +01003241 * bytes from the server, then this is the right moment. We have
3242 * to temporarily assign bytes_out to log what we currently have.
3243 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003244 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3245 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreau8b3977f2008-01-18 11:16:32 +01003246 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreau42250582007-04-01 01:30:43 +02003247 if (t->fe->to_log & LW_REQ)
3248 http_sess_log(t);
3249 else
3250 tcp_sess_log(t);
Krzysztof Piotr Oledzkif1e1cb42008-01-20 23:27:02 +01003251 t->logs.bytes_out = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003252 }
3253
Willy Tarreaua15645d2007-03-18 16:22:39 +01003254 /* Note: we must not try to cheat by jumping directly to DATA,
3255 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003256 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003257
3258 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003259 }
3260 else if (s == SV_STDATA) {
3261 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003262 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02003263 buffer_shutr(rep);
3264 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003265 fd_delete(t->srv_fd);
3266 if (t->srv) {
3267 t->srv->cur_sess--;
3268 t->srv->failed_resp++;
Willy Tarreau51406232008-03-10 22:04:20 +01003269 if (t->srv->proxy->lbprm.server_drop_conn)
3270 t->srv->proxy->lbprm.server_drop_conn(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003271 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003272 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003273 t->srv_state = SV_STCLOSE;
3274 if (!(t->flags & SN_ERR_MASK))
3275 t->flags |= SN_ERR_SRVCL;
3276 if (!(t->flags & SN_FINST_MASK))
3277 t->flags |= SN_FINST_D;
3278 /* We used to have a free connection slot. Since we'll never use it,
3279 * we have to inform the server that it may be used by another session.
3280 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003281 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003282 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003283
3284 return 1;
3285 }
3286 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003287 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003288 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003289 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003290 t->srv_state = SV_STSHUTR;
3291 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3292 return 1;
3293 }
3294 /* end of client read and no more data to send */
3295 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003296 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003297 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003298 shutdown(t->srv_fd, SHUT_WR);
3299 /* We must ensure that the read part is still alive when switching
3300 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003301 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003302 tv_add_ifset(&rep->rex, &now, &t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003303
3304 t->srv_state = SV_STSHUTW;
3305 return 1;
3306 }
3307 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003308 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003309 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003310 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003311 t->srv_state = SV_STSHUTR;
3312 if (!(t->flags & SN_ERR_MASK))
3313 t->flags |= SN_ERR_SRVTO;
3314 if (!(t->flags & SN_FINST_MASK))
3315 t->flags |= SN_FINST_D;
3316 return 1;
3317 }
3318 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003319 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003320 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003321 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003322 shutdown(t->srv_fd, SHUT_WR);
3323 /* We must ensure that the read part is still alive when switching
3324 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003325 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003326 tv_add_ifset(&rep->rex, &now, &t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003327 t->srv_state = SV_STSHUTW;
3328 if (!(t->flags & SN_ERR_MASK))
3329 t->flags |= SN_ERR_SRVTO;
3330 if (!(t->flags & SN_FINST_MASK))
3331 t->flags |= SN_FINST_D;
3332 return 1;
3333 }
3334
3335 /* recompute request time-outs */
3336 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003337 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3338 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003339 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003340 }
3341 }
3342 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau66319382007-04-08 17:17:37 +02003343 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3344 /* restart writing */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003345 if (tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003346 /* FIXME: to prevent the server from expiring read timeouts during writes,
3347 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003348 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003349 }
3350 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003351 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003352 }
3353 }
3354
3355 /* recompute response time-outs */
3356 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003357 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003358 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003359 }
3360 }
3361 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003362 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003363 if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server))
Willy Tarreaud7971282006-07-29 18:36:34 +02003364 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003365 }
3366 }
3367
3368 return 0; /* other cases change nothing */
3369 }
3370 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003371 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003372 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003373 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003374 fd_delete(t->srv_fd);
3375 if (t->srv) {
3376 t->srv->cur_sess--;
3377 t->srv->failed_resp++;
Willy Tarreau51406232008-03-10 22:04:20 +01003378 if (t->srv->proxy->lbprm.server_drop_conn)
3379 t->srv->proxy->lbprm.server_drop_conn(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003380 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003381 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003382 //close(t->srv_fd);
3383 t->srv_state = SV_STCLOSE;
3384 if (!(t->flags & SN_ERR_MASK))
3385 t->flags |= SN_ERR_SRVCL;
3386 if (!(t->flags & SN_FINST_MASK))
3387 t->flags |= SN_FINST_D;
3388 /* We used to have a free connection slot. Since we'll never use it,
3389 * we have to inform the server that it may be used by another session.
3390 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003391 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003392 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003393
3394 return 1;
3395 }
3396 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003397 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003398 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003399 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003400 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003401 t->srv->cur_sess--;
Willy Tarreau51406232008-03-10 22:04:20 +01003402 if (t->srv->proxy->lbprm.server_drop_conn)
3403 t->srv->proxy->lbprm.server_drop_conn(t->srv);
3404 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003405 //close(t->srv_fd);
3406 t->srv_state = SV_STCLOSE;
3407 /* We used to have a free connection slot. Since we'll never use it,
3408 * we have to inform the server that it may be used by another session.
3409 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003410 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003411 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003412
3413 return 1;
3414 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003415 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003416 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003417 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003418 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003419 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003420 t->srv->cur_sess--;
Willy Tarreau51406232008-03-10 22:04:20 +01003421 if (t->srv->proxy->lbprm.server_drop_conn)
3422 t->srv->proxy->lbprm.server_drop_conn(t->srv);
3423 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003424 //close(t->srv_fd);
3425 t->srv_state = SV_STCLOSE;
3426 if (!(t->flags & SN_ERR_MASK))
3427 t->flags |= SN_ERR_SRVTO;
3428 if (!(t->flags & SN_FINST_MASK))
3429 t->flags |= SN_FINST_D;
3430 /* We used to have a free connection slot. Since we'll never use it,
3431 * we have to inform the server that it may be used by another session.
3432 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003433 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003434 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003435
3436 return 1;
3437 }
3438 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003439 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3440 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003441 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003442 }
3443 }
3444 else { /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02003445 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3446 /* restart writing */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003447 if (!tv_add_ifset(&req->wex, &now, &t->be->timeout.server))
Willy Tarreaud7971282006-07-29 18:36:34 +02003448 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003449 }
3450 }
3451 return 0;
3452 }
3453 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003454 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003455 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003456 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003457 fd_delete(t->srv_fd);
3458 if (t->srv) {
3459 t->srv->cur_sess--;
3460 t->srv->failed_resp++;
Willy Tarreau51406232008-03-10 22:04:20 +01003461 if (t->srv->proxy->lbprm.server_drop_conn)
3462 t->srv->proxy->lbprm.server_drop_conn(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003463 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003464 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003465 //close(t->srv_fd);
3466 t->srv_state = SV_STCLOSE;
3467 if (!(t->flags & SN_ERR_MASK))
3468 t->flags |= SN_ERR_SRVCL;
3469 if (!(t->flags & SN_FINST_MASK))
3470 t->flags |= SN_FINST_D;
3471 /* We used to have a free connection slot. Since we'll never use it,
3472 * we have to inform the server that it may be used by another session.
3473 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003474 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003475 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003476
3477 return 1;
3478 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003479 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003480 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003481 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003482 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003483 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003484 t->srv->cur_sess--;
Willy Tarreau51406232008-03-10 22:04:20 +01003485 if (t->srv->proxy->lbprm.server_drop_conn)
3486 t->srv->proxy->lbprm.server_drop_conn(t->srv);
3487 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003488 //close(t->srv_fd);
3489 t->srv_state = SV_STCLOSE;
3490 /* We used to have a free connection slot. Since we'll never use it,
3491 * we have to inform the server that it may be used by another session.
3492 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003493 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003494 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003495
3496 return 1;
3497 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003498 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003499 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003500 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003501 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003502 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003503 t->srv->cur_sess--;
Willy Tarreau51406232008-03-10 22:04:20 +01003504 if (t->srv->proxy->lbprm.server_drop_conn)
3505 t->srv->proxy->lbprm.server_drop_conn(t->srv);
3506 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003507 //close(t->srv_fd);
3508 t->srv_state = SV_STCLOSE;
3509 if (!(t->flags & SN_ERR_MASK))
3510 t->flags |= SN_ERR_SRVTO;
3511 if (!(t->flags & SN_FINST_MASK))
3512 t->flags |= SN_FINST_D;
3513 /* We used to have a free connection slot. Since we'll never use it,
3514 * we have to inform the server that it may be used by another session.
3515 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003516 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003517 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003518
3519 return 1;
3520 }
3521 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003522 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003523 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003524 }
3525 }
3526 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003527 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003528 if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server))
Willy Tarreaud7971282006-07-29 18:36:34 +02003529 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003530 }
3531 }
3532 return 0;
3533 }
3534 else { /* SV_STCLOSE : nothing to do */
3535 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3536 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003537 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003538 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003539 write(1, trash, len);
3540 }
3541 return 0;
3542 }
3543 return 0;
3544}
3545
3546
3547/*
3548 * Produces data for the session <s> depending on its source. Expects to be
3549 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3550 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3551 * session, which it uses to keep on being called when there is free space in
3552 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3553 * if it changes the session state from CL_STSHUTR, otherwise 0.
3554 */
3555int produce_content(struct session *s)
3556{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003557 if (s->data_source == DATA_SRC_NONE) {
3558 s->flags &= ~SN_SELF_GEN;
3559 return 1;
3560 }
3561 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003562 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003563 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003564 if (ret >= 0)
3565 return ret;
3566 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003567 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003568
Willy Tarreau91861262007-10-17 17:06:05 +02003569 /* unknown data source or internal error */
3570 s->txn.status = 500;
3571 client_retnclose(s, error_message(s, HTTP_ERR_500));
3572 if (!(s->flags & SN_ERR_MASK))
3573 s->flags |= SN_ERR_PRXCOND;
3574 if (!(s->flags & SN_FINST_MASK))
3575 s->flags |= SN_FINST_R;
3576 s->flags &= ~SN_SELF_GEN;
3577 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003578}
3579
3580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003581/* Iterate the same filter through all request headers.
3582 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003583 * Since it can manage the switch to another backend, it updates the per-proxy
3584 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003585 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003586int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003587{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003588 char term;
3589 char *cur_ptr, *cur_end, *cur_next;
3590 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003591 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003592 struct hdr_idx_elem *cur_hdr;
3593 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003594
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003595 last_hdr = 0;
3596
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003597 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003598 old_idx = 0;
3599
3600 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003601 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003602 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003603 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003604 (exp->action == ACT_ALLOW ||
3605 exp->action == ACT_DENY ||
3606 exp->action == ACT_TARPIT))
3607 return 0;
3608
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003609 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003610 if (!cur_idx)
3611 break;
3612
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003613 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003614 cur_ptr = cur_next;
3615 cur_end = cur_ptr + cur_hdr->len;
3616 cur_next = cur_end + cur_hdr->cr + 1;
3617
3618 /* Now we have one header between cur_ptr and cur_end,
3619 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003620 */
3621
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003622 /* The annoying part is that pattern matching needs
3623 * that we modify the contents to null-terminate all
3624 * strings before testing them.
3625 */
3626
3627 term = *cur_end;
3628 *cur_end = '\0';
3629
3630 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3631 switch (exp->action) {
3632 case ACT_SETBE:
3633 /* It is not possible to jump a second time.
3634 * FIXME: should we return an HTTP/500 here so that
3635 * the admin knows there's a problem ?
3636 */
3637 if (t->be != t->fe)
3638 break;
3639
3640 /* Swithing Proxy */
3641 t->be = (struct proxy *) exp->replace;
3642
3643 /* right now, the backend switch is not too much complicated
3644 * because we have associated req_cap and rsp_cap to the
3645 * frontend, and the beconn will be updated later.
3646 */
3647
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003648 t->rep->rto = t->req->wto = t->be->timeout.server;
3649 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003650 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003651 last_hdr = 1;
3652 break;
3653
3654 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003655 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003656 last_hdr = 1;
3657 break;
3658
3659 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003660 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003661 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003662 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003663 break;
3664
3665 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003666 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003667 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003668 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003669 break;
3670
3671 case ACT_REPLACE:
3672 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3673 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3674 /* FIXME: if the user adds a newline in the replacement, the
3675 * index will not be recalculated for now, and the new line
3676 * will not be counted as a new header.
3677 */
3678
3679 cur_end += delta;
3680 cur_next += delta;
3681 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003682 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003683 break;
3684
3685 case ACT_REMOVE:
3686 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3687 cur_next += delta;
3688
3689 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003690 txn->req.eoh += delta;
3691 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3692 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003693 cur_hdr->len = 0;
3694 cur_end = NULL; /* null-term has been rewritten */
3695 break;
3696
3697 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003698 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003699 if (cur_end)
3700 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003701
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003702 /* keep the link from this header to next one in case of later
3703 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003704 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003705 old_idx = cur_idx;
3706 }
3707 return 0;
3708}
3709
3710
3711/* Apply the filter to the request line.
3712 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3713 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003714 * Since it can manage the switch to another backend, it updates the per-proxy
3715 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003716 */
3717int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3718{
3719 char term;
3720 char *cur_ptr, *cur_end;
3721 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003722 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003723 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003724
Willy Tarreau58f10d72006-12-04 02:26:12 +01003725
Willy Tarreau3d300592007-03-18 18:34:41 +01003726 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003727 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003728 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003729 (exp->action == ACT_ALLOW ||
3730 exp->action == ACT_DENY ||
3731 exp->action == ACT_TARPIT))
3732 return 0;
3733 else if (exp->action == ACT_REMOVE)
3734 return 0;
3735
3736 done = 0;
3737
Willy Tarreau9cdde232007-05-02 20:58:19 +02003738 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003739 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003740
3741 /* Now we have the request line between cur_ptr and cur_end */
3742
3743 /* The annoying part is that pattern matching needs
3744 * that we modify the contents to null-terminate all
3745 * strings before testing them.
3746 */
3747
3748 term = *cur_end;
3749 *cur_end = '\0';
3750
3751 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3752 switch (exp->action) {
3753 case ACT_SETBE:
3754 /* It is not possible to jump a second time.
3755 * FIXME: should we return an HTTP/500 here so that
3756 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003757 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003758 if (t->be != t->fe)
3759 break;
3760
3761 /* Swithing Proxy */
3762 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003763
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003764 /* right now, the backend switch is not too much complicated
3765 * because we have associated req_cap and rsp_cap to the
3766 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003767 */
3768
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003769 t->rep->rto = t->req->wto = t->be->timeout.server;
3770 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003771 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003772 done = 1;
3773 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003774
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003775 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003776 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003777 done = 1;
3778 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003779
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003780 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003781 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003782 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003783 done = 1;
3784 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003785
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003786 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003787 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003788 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003789 done = 1;
3790 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003791
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003792 case ACT_REPLACE:
3793 *cur_end = term; /* restore the string terminator */
3794 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3795 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3796 /* FIXME: if the user adds a newline in the replacement, the
3797 * index will not be recalculated for now, and the new line
3798 * will not be counted as a new header.
3799 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003800
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003801 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003802 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003803
Willy Tarreau9cdde232007-05-02 20:58:19 +02003804 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003805 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003806 HTTP_MSG_RQMETH,
3807 cur_ptr, cur_end + 1,
3808 NULL, NULL);
3809 if (unlikely(!cur_end))
3810 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003811
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003812 /* we have a full request and we know that we have either a CR
3813 * or an LF at <ptr>.
3814 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003815 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3816 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003817 /* there is no point trying this regex on headers */
3818 return 1;
3819 }
3820 }
3821 *cur_end = term; /* restore the string terminator */
3822 return done;
3823}
Willy Tarreau97de6242006-12-27 17:18:38 +01003824
Willy Tarreau58f10d72006-12-04 02:26:12 +01003825
Willy Tarreau58f10d72006-12-04 02:26:12 +01003826
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003827/*
3828 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3829 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003830 * unparsable request. Since it can manage the switch to another backend, it
3831 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003832 */
3833int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3834{
Willy Tarreau3d300592007-03-18 18:34:41 +01003835 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003836 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003837 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003838 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003839
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003840 /*
3841 * The interleaving of transformations and verdicts
3842 * makes it difficult to decide to continue or stop
3843 * the evaluation.
3844 */
3845
Willy Tarreau3d300592007-03-18 18:34:41 +01003846 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003847 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3848 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3849 exp = exp->next;
3850 continue;
3851 }
3852
3853 /* Apply the filter to the request line. */
3854 ret = apply_filter_to_req_line(t, req, exp);
3855 if (unlikely(ret < 0))
3856 return -1;
3857
3858 if (likely(ret == 0)) {
3859 /* The filter did not match the request, it can be
3860 * iterated through all headers.
3861 */
3862 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003863 }
3864 exp = exp->next;
3865 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003866 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003867}
3868
3869
Willy Tarreaua15645d2007-03-18 16:22:39 +01003870
Willy Tarreau58f10d72006-12-04 02:26:12 +01003871/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003872 * Manage client-side cookie. It can impact performance by about 2% so it is
3873 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003874 */
3875void manage_client_side_cookies(struct session *t, struct buffer *req)
3876{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003877 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003878 char *p1, *p2, *p3, *p4;
3879 char *del_colon, *del_cookie, *colon;
3880 int app_cookies;
3881
3882 appsess *asession_temp = NULL;
3883 appsess local_asession;
3884
3885 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003886 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003887
Willy Tarreau2a324282006-12-05 00:05:46 +01003888 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003889 * we start with the start line.
3890 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003891 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003892 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003893
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003894 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003895 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003896 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003897
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003898 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003899 cur_ptr = cur_next;
3900 cur_end = cur_ptr + cur_hdr->len;
3901 cur_next = cur_end + cur_hdr->cr + 1;
3902
3903 /* We have one full header between cur_ptr and cur_end, and the
3904 * next header starts at cur_next. We're only interested in
3905 * "Cookie:" headers.
3906 */
3907
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003908 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3909 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003910 old_idx = cur_idx;
3911 continue;
3912 }
3913
3914 /* Now look for cookies. Conforming to RFC2109, we have to support
3915 * attributes whose name begin with a '$', and associate them with
3916 * the right cookie, if we want to delete this cookie.
3917 * So there are 3 cases for each cookie read :
3918 * 1) it's a special attribute, beginning with a '$' : ignore it.
3919 * 2) it's a server id cookie that we *MAY* want to delete : save
3920 * some pointers on it (last semi-colon, beginning of cookie...)
3921 * 3) it's an application cookie : we *MAY* have to delete a previous
3922 * "special" cookie.
3923 * At the end of loop, if a "special" cookie remains, we may have to
3924 * remove it. If no application cookie persists in the header, we
3925 * *MUST* delete it
3926 */
3927
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003928 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003929
Willy Tarreau58f10d72006-12-04 02:26:12 +01003930 /* del_cookie == NULL => nothing to be deleted */
3931 del_colon = del_cookie = NULL;
3932 app_cookies = 0;
3933
3934 while (p1 < cur_end) {
3935 /* skip spaces and colons, but keep an eye on these ones */
3936 while (p1 < cur_end) {
3937 if (*p1 == ';' || *p1 == ',')
3938 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003939 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003940 break;
3941 p1++;
3942 }
3943
3944 if (p1 == cur_end)
3945 break;
3946
3947 /* p1 is at the beginning of the cookie name */
3948 p2 = p1;
3949 while (p2 < cur_end && *p2 != '=')
3950 p2++;
3951
3952 if (p2 == cur_end)
3953 break;
3954
3955 p3 = p2 + 1; /* skips the '=' sign */
3956 if (p3 == cur_end)
3957 break;
3958
3959 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003960 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003961 p4++;
3962
3963 /* here, we have the cookie name between p1 and p2,
3964 * and its value between p3 and p4.
3965 * we can process it :
3966 *
3967 * Cookie: NAME=VALUE;
3968 * | || || |
3969 * | || || +--> p4
3970 * | || |+-------> p3
3971 * | || +--------> p2
3972 * | |+------------> p1
3973 * | +-------------> colon
3974 * +--------------------> cur_ptr
3975 */
3976
3977 if (*p1 == '$') {
3978 /* skip this one */
3979 }
3980 else {
3981 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003982 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003983 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003984 (p4 - p1 >= t->fe->capture_namelen) &&
3985 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003986 int log_len = p4 - p1;
3987
Willy Tarreau086b3b42007-05-13 21:45:51 +02003988 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003989 Alert("HTTP logging : out of memory.\n");
3990 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003991 if (log_len > t->fe->capture_len)
3992 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003993 memcpy(txn->cli_cookie, p1, log_len);
3994 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003995 }
3996 }
3997
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003998 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3999 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004000 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004001 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004002 char *delim;
4003
4004 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4005 * have the server ID betweek p3 and delim, and the original cookie between
4006 * delim+1 and p4. Otherwise, delim==p4 :
4007 *
4008 * Cookie: NAME=SRV~VALUE;
4009 * | || || | |
4010 * | || || | +--> p4
4011 * | || || +--------> delim
4012 * | || |+-----------> p3
4013 * | || +------------> p2
4014 * | |+----------------> p1
4015 * | +-----------------> colon
4016 * +------------------------> cur_ptr
4017 */
4018
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004019 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004020 for (delim = p3; delim < p4; delim++)
4021 if (*delim == COOKIE_DELIM)
4022 break;
4023 }
4024 else
4025 delim = p4;
4026
4027
4028 /* Here, we'll look for the first running server which supports the cookie.
4029 * This allows to share a same cookie between several servers, for example
4030 * to dedicate backup servers to specific servers only.
4031 * However, to prevent clients from sticking to cookie-less backup server
4032 * when they have incidentely learned an empty cookie, we simply ignore
4033 * empty cookies and mark them as invalid.
4034 */
4035 if (delim == p3)
4036 srv = NULL;
4037
4038 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004039 if (srv->cookie && (srv->cklen == delim - p3) &&
4040 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004041 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004042 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004043 txn->flags &= ~TX_CK_MASK;
4044 txn->flags |= TX_CK_VALID;
4045 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004046 t->srv = srv;
4047 break;
4048 } else {
4049 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004050 txn->flags &= ~TX_CK_MASK;
4051 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004052 }
4053 }
4054 srv = srv->next;
4055 }
4056
Willy Tarreau3d300592007-03-18 18:34:41 +01004057 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004058 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004059 txn->flags &= ~TX_CK_MASK;
4060 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004061 }
4062
4063 /* depending on the cookie mode, we may have to either :
4064 * - delete the complete cookie if we're in insert+indirect mode, so that
4065 * the server never sees it ;
4066 * - remove the server id from the cookie value, and tag the cookie as an
4067 * application cookie so that it does not get accidentely removed later,
4068 * if we're in cookie prefix mode
4069 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004070 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004071 int delta; /* negative */
4072
4073 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4074 p4 += delta;
4075 cur_end += delta;
4076 cur_next += delta;
4077 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004078 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004079
4080 del_cookie = del_colon = NULL;
4081 app_cookies++; /* protect the header from deletion */
4082 }
4083 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004084 (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 +01004085 del_cookie = p1;
4086 del_colon = colon;
4087 }
4088 } else {
4089 /* now we know that we must keep this cookie since it's
4090 * not ours. But if we wanted to delete our cookie
4091 * earlier, we cannot remove the complete header, but we
4092 * can remove the previous block itself.
4093 */
4094 app_cookies++;
4095
4096 if (del_cookie != NULL) {
4097 int delta; /* negative */
4098
4099 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4100 p4 += delta;
4101 cur_end += delta;
4102 cur_next += delta;
4103 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004104 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004105 del_cookie = del_colon = NULL;
4106 }
4107 }
4108
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004109 if ((t->be->appsession_name != NULL) &&
4110 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004111 /* first, let's see if the cookie is our appcookie*/
4112
4113 /* Cool... it's the right one */
4114
4115 asession_temp = &local_asession;
4116
Willy Tarreau63963c62007-05-13 21:29:55 +02004117 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004118 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4119 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4120 return;
4121 }
4122
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004123 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4124 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004125 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004126
Willy Tarreau58f10d72006-12-04 02:26:12 +01004127 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004128 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4129 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004130 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004131 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004132 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004133 Alert("Not enough memory process_cli():asession:calloc().\n");
4134 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4135 return;
4136 }
4137
4138 asession_temp->sessid = local_asession.sessid;
4139 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02004140 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004141 } else {
4142 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004143 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004144 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004145 if (asession_temp->serverid == NULL) {
4146 Alert("Found Application Session without matching server.\n");
4147 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004148 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004149 while (srv) {
4150 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004151 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004152 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004153 txn->flags &= ~TX_CK_MASK;
4154 txn->flags |= TX_CK_VALID;
4155 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004156 t->srv = srv;
4157 break;
4158 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004159 txn->flags &= ~TX_CK_MASK;
4160 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004161 }
4162 }
4163 srv = srv->next;
4164 }/* end while(srv) */
4165 }/* end else if server == NULL */
4166
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004167 tv_add(&asession_temp->expire, &now, &t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004168 }/* end if ((t->proxy->appsession_name != NULL) ... */
4169 }
4170
4171 /* we'll have to look for another cookie ... */
4172 p1 = p4;
4173 } /* while (p1 < cur_end) */
4174
4175 /* There's no more cookie on this line.
4176 * We may have marked the last one(s) for deletion.
4177 * We must do this now in two ways :
4178 * - if there is no app cookie, we simply delete the header ;
4179 * - if there are app cookies, we must delete the end of the
4180 * string properly, including the colon/semi-colon before
4181 * the cookie name.
4182 */
4183 if (del_cookie != NULL) {
4184 int delta;
4185 if (app_cookies) {
4186 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4187 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004188 cur_hdr->len += delta;
4189 } else {
4190 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004191
4192 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004193 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4194 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004195 cur_hdr->len = 0;
4196 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004197 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004198 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004199 }
4200
4201 /* keep the link from this header to next one */
4202 old_idx = cur_idx;
4203 } /* end of cookie processing on this header */
4204}
4205
4206
Willy Tarreaua15645d2007-03-18 16:22:39 +01004207/* Iterate the same filter through all response headers contained in <rtr>.
4208 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4209 */
4210int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4211{
4212 char term;
4213 char *cur_ptr, *cur_end, *cur_next;
4214 int cur_idx, old_idx, last_hdr;
4215 struct http_txn *txn = &t->txn;
4216 struct hdr_idx_elem *cur_hdr;
4217 int len, delta;
4218
4219 last_hdr = 0;
4220
4221 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4222 old_idx = 0;
4223
4224 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004225 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004226 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004227 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004228 (exp->action == ACT_ALLOW ||
4229 exp->action == ACT_DENY))
4230 return 0;
4231
4232 cur_idx = txn->hdr_idx.v[old_idx].next;
4233 if (!cur_idx)
4234 break;
4235
4236 cur_hdr = &txn->hdr_idx.v[cur_idx];
4237 cur_ptr = cur_next;
4238 cur_end = cur_ptr + cur_hdr->len;
4239 cur_next = cur_end + cur_hdr->cr + 1;
4240
4241 /* Now we have one header between cur_ptr and cur_end,
4242 * and the next header starts at cur_next.
4243 */
4244
4245 /* The annoying part is that pattern matching needs
4246 * that we modify the contents to null-terminate all
4247 * strings before testing them.
4248 */
4249
4250 term = *cur_end;
4251 *cur_end = '\0';
4252
4253 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4254 switch (exp->action) {
4255 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004256 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004257 last_hdr = 1;
4258 break;
4259
4260 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004261 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004262 last_hdr = 1;
4263 break;
4264
4265 case ACT_REPLACE:
4266 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4267 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4268 /* FIXME: if the user adds a newline in the replacement, the
4269 * index will not be recalculated for now, and the new line
4270 * will not be counted as a new header.
4271 */
4272
4273 cur_end += delta;
4274 cur_next += delta;
4275 cur_hdr->len += delta;
4276 txn->rsp.eoh += delta;
4277 break;
4278
4279 case ACT_REMOVE:
4280 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4281 cur_next += delta;
4282
4283 /* FIXME: this should be a separate function */
4284 txn->rsp.eoh += delta;
4285 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4286 txn->hdr_idx.used--;
4287 cur_hdr->len = 0;
4288 cur_end = NULL; /* null-term has been rewritten */
4289 break;
4290
4291 }
4292 }
4293 if (cur_end)
4294 *cur_end = term; /* restore the string terminator */
4295
4296 /* keep the link from this header to next one in case of later
4297 * removal of next header.
4298 */
4299 old_idx = cur_idx;
4300 }
4301 return 0;
4302}
4303
4304
4305/* Apply the filter to the status line in the response buffer <rtr>.
4306 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4307 * or -1 if a replacement resulted in an invalid status line.
4308 */
4309int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4310{
4311 char term;
4312 char *cur_ptr, *cur_end;
4313 int done;
4314 struct http_txn *txn = &t->txn;
4315 int len, delta;
4316
4317
Willy Tarreau3d300592007-03-18 18:34:41 +01004318 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004319 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004320 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004321 (exp->action == ACT_ALLOW ||
4322 exp->action == ACT_DENY))
4323 return 0;
4324 else if (exp->action == ACT_REMOVE)
4325 return 0;
4326
4327 done = 0;
4328
Willy Tarreau9cdde232007-05-02 20:58:19 +02004329 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004330 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4331
4332 /* Now we have the status line between cur_ptr and cur_end */
4333
4334 /* The annoying part is that pattern matching needs
4335 * that we modify the contents to null-terminate all
4336 * strings before testing them.
4337 */
4338
4339 term = *cur_end;
4340 *cur_end = '\0';
4341
4342 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4343 switch (exp->action) {
4344 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004345 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004346 done = 1;
4347 break;
4348
4349 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004350 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004351 done = 1;
4352 break;
4353
4354 case ACT_REPLACE:
4355 *cur_end = term; /* restore the string terminator */
4356 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4357 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4358 /* FIXME: if the user adds a newline in the replacement, the
4359 * index will not be recalculated for now, and the new line
4360 * will not be counted as a new header.
4361 */
4362
4363 txn->rsp.eoh += delta;
4364 cur_end += delta;
4365
Willy Tarreau9cdde232007-05-02 20:58:19 +02004366 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004367 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004368 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004369 cur_ptr, cur_end + 1,
4370 NULL, NULL);
4371 if (unlikely(!cur_end))
4372 return -1;
4373
4374 /* we have a full respnse and we know that we have either a CR
4375 * or an LF at <ptr>.
4376 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004377 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004378 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4379 /* there is no point trying this regex on headers */
4380 return 1;
4381 }
4382 }
4383 *cur_end = term; /* restore the string terminator */
4384 return done;
4385}
4386
4387
4388
4389/*
4390 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4391 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4392 * unparsable response.
4393 */
4394int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4395{
Willy Tarreau3d300592007-03-18 18:34:41 +01004396 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004397 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004398 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004399 int ret;
4400
4401 /*
4402 * The interleaving of transformations and verdicts
4403 * makes it difficult to decide to continue or stop
4404 * the evaluation.
4405 */
4406
Willy Tarreau3d300592007-03-18 18:34:41 +01004407 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004408 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4409 exp->action == ACT_PASS)) {
4410 exp = exp->next;
4411 continue;
4412 }
4413
4414 /* Apply the filter to the status line. */
4415 ret = apply_filter_to_sts_line(t, rtr, exp);
4416 if (unlikely(ret < 0))
4417 return -1;
4418
4419 if (likely(ret == 0)) {
4420 /* The filter did not match the response, it can be
4421 * iterated through all headers.
4422 */
4423 apply_filter_to_resp_headers(t, rtr, exp);
4424 }
4425 exp = exp->next;
4426 }
4427 return 0;
4428}
4429
4430
4431
4432/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004433 * Manage server-side cookies. It can impact performance by about 2% so it is
4434 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004435 */
4436void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4437{
4438 struct http_txn *txn = &t->txn;
4439 char *p1, *p2, *p3, *p4;
4440
4441 appsess *asession_temp = NULL;
4442 appsess local_asession;
4443
4444 char *cur_ptr, *cur_end, *cur_next;
4445 int cur_idx, old_idx, delta;
4446
Willy Tarreaua15645d2007-03-18 16:22:39 +01004447 /* Iterate through the headers.
4448 * we start with the start line.
4449 */
4450 old_idx = 0;
4451 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4452
4453 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4454 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004455 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004456
4457 cur_hdr = &txn->hdr_idx.v[cur_idx];
4458 cur_ptr = cur_next;
4459 cur_end = cur_ptr + cur_hdr->len;
4460 cur_next = cur_end + cur_hdr->cr + 1;
4461
4462 /* We have one full header between cur_ptr and cur_end, and the
4463 * next header starts at cur_next. We're only interested in
4464 * "Cookie:" headers.
4465 */
4466
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004467 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4468 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004469 old_idx = cur_idx;
4470 continue;
4471 }
4472
4473 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004474 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004475
4476
4477 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004478 if (t->be->cookie_name == NULL &&
4479 t->be->appsession_name == NULL &&
4480 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004481 return;
4482
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004483 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004484
4485 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004486 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4487 break;
4488
4489 /* p1 is at the beginning of the cookie name */
4490 p2 = p1;
4491
4492 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4493 p2++;
4494
4495 if (p2 == cur_end || *p2 == ';') /* next cookie */
4496 break;
4497
4498 p3 = p2 + 1; /* skip the '=' sign */
4499 if (p3 == cur_end)
4500 break;
4501
4502 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004503 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004504 p4++;
4505
4506 /* here, we have the cookie name between p1 and p2,
4507 * and its value between p3 and p4.
4508 * we can process it.
4509 */
4510
4511 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004512 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004513 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004514 (p4 - p1 >= t->be->capture_namelen) &&
4515 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004516 int log_len = p4 - p1;
4517
Willy Tarreau086b3b42007-05-13 21:45:51 +02004518 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004519 Alert("HTTP logging : out of memory.\n");
4520 }
4521
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004522 if (log_len > t->be->capture_len)
4523 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004524 memcpy(txn->srv_cookie, p1, log_len);
4525 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004526 }
4527
4528 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004529 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4530 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004531 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004532 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004533
4534 /* If the cookie is in insert mode on a known server, we'll delete
4535 * this occurrence because we'll insert another one later.
4536 * We'll delete it too if the "indirect" option is set and we're in
4537 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004538 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4539 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004540 /* this header must be deleted */
4541 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4542 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4543 txn->hdr_idx.used--;
4544 cur_hdr->len = 0;
4545 cur_next += delta;
4546 txn->rsp.eoh += delta;
4547
Willy Tarreau3d300592007-03-18 18:34:41 +01004548 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004549 }
4550 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004551 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004552 /* replace bytes p3->p4 with the cookie name associated
4553 * with this server since we know it.
4554 */
4555 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4556 cur_hdr->len += delta;
4557 cur_next += delta;
4558 txn->rsp.eoh += delta;
4559
Willy Tarreau3d300592007-03-18 18:34:41 +01004560 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004561 }
4562 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004563 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004564 /* insert the cookie name associated with this server
4565 * before existing cookie, and insert a delimitor between them..
4566 */
4567 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4568 cur_hdr->len += delta;
4569 cur_next += delta;
4570 txn->rsp.eoh += delta;
4571
4572 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004573 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004574 }
4575 }
4576 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004577 else if ((t->be->appsession_name != NULL) &&
4578 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004579
4580 /* Cool... it's the right one */
4581
4582 size_t server_id_len = strlen(t->srv->id) + 1;
4583 asession_temp = &local_asession;
4584
Willy Tarreau63963c62007-05-13 21:29:55 +02004585 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004586 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4587 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4588 return;
4589 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004590 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4591 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004592 asession_temp->serverid = NULL;
4593
4594 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004595 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4596 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004597 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004598 Alert("Not enough Memory process_srv():asession:calloc().\n");
4599 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4600 return;
4601 }
4602 asession_temp->sessid = local_asession.sessid;
4603 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02004604 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4605 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004606 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004607 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004608 }
4609
Willy Tarreaua15645d2007-03-18 16:22:39 +01004610 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004611 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004612 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4613 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4614 return;
4615 }
4616 asession_temp->serverid[0] = '\0';
4617 }
4618
4619 if (asession_temp->serverid[0] == '\0')
4620 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4621
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004622 tv_add(&asession_temp->expire, &now, &t->be->timeout.appsession);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004623
4624#if defined(DEBUG_HASH)
Willy Tarreau51041c72007-09-09 21:56:53 +02004625 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004626#endif
4627 }/* end if ((t->proxy->appsession_name != NULL) ... */
4628 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4629 } /* we're now at the end of the cookie value */
4630
4631 /* keep the link from this header to next one */
4632 old_idx = cur_idx;
4633 } /* end of cookie processing on this header */
4634}
4635
4636
4637
4638/*
4639 * Check if response is cacheable or not. Updates t->flags.
4640 */
4641void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4642{
4643 struct http_txn *txn = &t->txn;
4644 char *p1, *p2;
4645
4646 char *cur_ptr, *cur_end, *cur_next;
4647 int cur_idx;
4648
Willy Tarreau5df51872007-11-25 16:20:08 +01004649 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004650 return;
4651
4652 /* Iterate through the headers.
4653 * we start with the start line.
4654 */
4655 cur_idx = 0;
4656 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4657
4658 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4659 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004660 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004661
4662 cur_hdr = &txn->hdr_idx.v[cur_idx];
4663 cur_ptr = cur_next;
4664 cur_end = cur_ptr + cur_hdr->len;
4665 cur_next = cur_end + cur_hdr->cr + 1;
4666
4667 /* We have one full header between cur_ptr and cur_end, and the
4668 * next header starts at cur_next. We're only interested in
4669 * "Cookie:" headers.
4670 */
4671
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004672 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4673 if (val) {
4674 if ((cur_end - (cur_ptr + val) >= 8) &&
4675 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4676 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4677 return;
4678 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004679 }
4680
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004681 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4682 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004683 continue;
4684
4685 /* OK, right now we know we have a cache-control header at cur_ptr */
4686
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004687 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004688
4689 if (p1 >= cur_end) /* no more info */
4690 continue;
4691
4692 /* p1 is at the beginning of the value */
4693 p2 = p1;
4694
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004695 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004696 p2++;
4697
4698 /* we have a complete value between p1 and p2 */
4699 if (p2 < cur_end && *p2 == '=') {
4700 /* we have something of the form no-cache="set-cookie" */
4701 if ((cur_end - p1 >= 21) &&
4702 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4703 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004704 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004705 continue;
4706 }
4707
4708 /* OK, so we know that either p2 points to the end of string or to a comma */
4709 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4710 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4711 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4712 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004713 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004714 return;
4715 }
4716
4717 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004718 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004719 continue;
4720 }
4721 }
4722}
4723
4724
Willy Tarreau58f10d72006-12-04 02:26:12 +01004725/*
4726 * Try to retrieve a known appsession in the URI, then the associated server.
4727 * If the server is found, it's assigned to the session.
4728 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004729void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004730{
Willy Tarreau3d300592007-03-18 18:34:41 +01004731 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004732 appsess *asession_temp = NULL;
4733 appsess local_asession;
4734 char *request_line;
4735
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004736 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004737 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004738 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004739 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004740 return;
4741
4742 /* skip ';' */
4743 request_line++;
4744
4745 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004746 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004747 return;
4748
4749 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004750 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004751
4752 /* First try if we already have an appsession */
4753 asession_temp = &local_asession;
4754
Willy Tarreau63963c62007-05-13 21:29:55 +02004755 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004756 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4757 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4758 return;
4759 }
4760
4761 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004762 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4763 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004764 asession_temp->serverid = NULL;
4765
4766 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004767 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4768 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004769 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004770 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004771 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004772 Alert("Not enough memory process_cli():asession:calloc().\n");
4773 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4774 return;
4775 }
4776 asession_temp->sessid = local_asession.sessid;
4777 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02004778 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004779 }
4780 else {
4781 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004782 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004783 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004784
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004785 tv_add(&asession_temp->expire, &now, &t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004786 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004787
Willy Tarreau58f10d72006-12-04 02:26:12 +01004788#if defined(DEBUG_HASH)
Willy Tarreau51041c72007-09-09 21:56:53 +02004789 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004790#endif
4791 if (asession_temp->serverid == NULL) {
4792 Alert("Found Application Session without matching server.\n");
4793 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004794 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004795 while (srv) {
4796 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004797 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004798 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004799 txn->flags &= ~TX_CK_MASK;
4800 txn->flags |= TX_CK_VALID;
4801 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004802 t->srv = srv;
4803 break;
4804 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004805 txn->flags &= ~TX_CK_MASK;
4806 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004807 }
4808 }
4809 srv = srv->next;
4810 }
4811 }
4812}
4813
4814
Willy Tarreaub2513902006-12-17 14:52:38 +01004815/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004816 * In a GET or HEAD request, check if the requested URI matches the stats uri
4817 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004818 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004819 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004820 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004821 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004822 *
4823 * Returns 1 if the session's state changes, otherwise 0.
4824 */
4825int stats_check_uri_auth(struct session *t, struct proxy *backend)
4826{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004827 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004828 struct uri_auth *uri_auth = backend->uri_auth;
4829 struct user_auth *user;
4830 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004831 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004832
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004833 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
4834
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004835 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004836 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004837 return 0;
4838
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004839 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004840
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004841 /* the URI is in h */
4842 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004843 return 0;
4844
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004845 h += uri_auth->uri_len;
4846 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4847 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004848 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004849 break;
4850 }
4851 h++;
4852 }
4853
4854 if (uri_auth->refresh) {
4855 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4856 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4857 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004858 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004859 break;
4860 }
4861 h++;
4862 }
4863 }
4864
Willy Tarreau55bb8452007-10-17 18:44:57 +02004865 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4866 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4867 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004868 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02004869 break;
4870 }
4871 h++;
4872 }
4873
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004874 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
4875
Willy Tarreaub2513902006-12-17 14:52:38 +01004876 /* we are in front of a interceptable URI. Let's check
4877 * if there's an authentication and if it's valid.
4878 */
4879 user = uri_auth->users;
4880 if (!user) {
4881 /* no user auth required, it's OK */
4882 authenticated = 1;
4883 } else {
4884 authenticated = 0;
4885
4886 /* a user list is defined, we have to check.
4887 * skip 21 chars for "Authorization: Basic ".
4888 */
4889
4890 /* FIXME: this should move to an earlier place */
4891 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004892 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4893 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4894 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004895 if (len > 14 &&
4896 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004897 txn->auth_hdr.str = h;
4898 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004899 break;
4900 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004901 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004902 }
4903
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004904 if (txn->auth_hdr.len < 21 ||
4905 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004906 user = NULL;
4907
4908 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004909 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4910 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004911 user->user_pwd, user->user_len)) {
4912 authenticated = 1;
4913 break;
4914 }
4915 user = user->next;
4916 }
4917 }
4918
4919 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004920 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004921
4922 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004923 msg.str = trash;
4924 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004925 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01004926 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01004927 if (!(t->flags & SN_ERR_MASK))
4928 t->flags |= SN_ERR_PRXCOND;
4929 if (!(t->flags & SN_FINST_MASK))
4930 t->flags |= SN_FINST_R;
4931 return 1;
4932 }
4933
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004934 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01004935 * data.
4936 */
4937 t->cli_state = CL_STSHUTR;
4938 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02004939 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub2513902006-12-17 14:52:38 +01004940 t->data_source = DATA_SRC_STATS;
4941 t->data_state = DATA_ST_INIT;
4942 produce_content(t);
4943 return 1;
4944}
4945
4946
Willy Tarreaubaaee002006-06-26 02:48:02 +02004947/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004948 * Print a debug line with a header
4949 */
4950void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4951{
4952 int len, max;
4953 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
4954 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
4955 max = end - start;
4956 UBOUND(max, sizeof(trash) - len - 1);
4957 len += strlcpy2(trash + len, start, max + 1);
4958 trash[len++] = '\n';
4959 write(1, trash, len);
4960}
4961
4962
Willy Tarreau8797c062007-05-07 00:55:35 +02004963/************************************************************************/
4964/* The code below is dedicated to ACL parsing and matching */
4965/************************************************************************/
4966
4967
4968
4969
4970/* 1. Check on METHOD
4971 * We use the pre-parsed method if it is known, and store its number as an
4972 * integer. If it is unknown, we use the pointer and the length.
4973 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004974static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004975{
4976 int len, meth;
4977
Willy Tarreauae8b7962007-06-09 23:10:04 +02004978 len = strlen(*text);
4979 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004980
4981 pattern->val.i = meth;
4982 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004983 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004984 if (!pattern->ptr.str)
4985 return 0;
4986 pattern->len = len;
4987 }
4988 return 1;
4989}
4990
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004991static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004992acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4993 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004994{
4995 int meth;
4996 struct http_txn *txn = l7;
4997
Willy Tarreauc11416f2007-06-17 16:58:38 +02004998 if (txn->req.msg_state != HTTP_MSG_BODY)
4999 return 0;
5000
Willy Tarreau8797c062007-05-07 00:55:35 +02005001 meth = txn->meth;
5002 test->i = meth;
5003 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005004 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5005 /* ensure the indexes are not affected */
5006 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005007 test->len = txn->req.sl.rq.m_l;
5008 test->ptr = txn->req.sol;
5009 }
5010 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5011 return 1;
5012}
5013
5014static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5015{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005016 int icase;
5017
Willy Tarreau8797c062007-05-07 00:55:35 +02005018 if (test->i != pattern->val.i)
5019 return 0;
5020
5021 if (test->i != HTTP_METH_OTHER)
5022 return 1;
5023
5024 /* Other method, we must compare the strings */
5025 if (pattern->len != test->len)
5026 return 0;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005027
5028 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5029 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5030 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau8797c062007-05-07 00:55:35 +02005031 return 0;
5032 return 1;
5033}
5034
5035/* 2. Check on Request/Status Version
5036 * We simply compare strings here.
5037 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005038static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005039{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005040 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005041 if (!pattern->ptr.str)
5042 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005043 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005044 return 1;
5045}
5046
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005047static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005048acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5049 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005050{
5051 struct http_txn *txn = l7;
5052 char *ptr;
5053 int len;
5054
Willy Tarreauc11416f2007-06-17 16:58:38 +02005055 if (txn->req.msg_state != HTTP_MSG_BODY)
5056 return 0;
5057
Willy Tarreau8797c062007-05-07 00:55:35 +02005058 len = txn->req.sl.rq.v_l;
5059 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5060
5061 while ((len-- > 0) && (*ptr++ != '/'));
5062 if (len <= 0)
5063 return 0;
5064
5065 test->ptr = ptr;
5066 test->len = len;
5067
5068 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5069 return 1;
5070}
5071
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005072static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005073acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5074 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005075{
5076 struct http_txn *txn = l7;
5077 char *ptr;
5078 int len;
5079
Willy Tarreauc11416f2007-06-17 16:58:38 +02005080 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5081 return 0;
5082
Willy Tarreau8797c062007-05-07 00:55:35 +02005083 len = txn->rsp.sl.st.v_l;
5084 ptr = txn->rsp.sol;
5085
5086 while ((len-- > 0) && (*ptr++ != '/'));
5087 if (len <= 0)
5088 return 0;
5089
5090 test->ptr = ptr;
5091 test->len = len;
5092
5093 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5094 return 1;
5095}
5096
5097/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005098static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005099acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5100 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005101{
5102 struct http_txn *txn = l7;
5103 char *ptr;
5104 int len;
5105
Willy Tarreauc11416f2007-06-17 16:58:38 +02005106 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5107 return 0;
5108
Willy Tarreau8797c062007-05-07 00:55:35 +02005109 len = txn->rsp.sl.st.c_l;
5110 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5111
5112 test->i = __strl2ui(ptr, len);
5113 test->flags = ACL_TEST_F_VOL_1ST;
5114 return 1;
5115}
5116
5117/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005118static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005119acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5120 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005121{
5122 struct http_txn *txn = l7;
5123
Willy Tarreauc11416f2007-06-17 16:58:38 +02005124 if (txn->req.msg_state != HTTP_MSG_BODY)
5125 return 0;
5126 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5127 /* ensure the indexes are not affected */
5128 return 0;
5129
Willy Tarreau8797c062007-05-07 00:55:35 +02005130 test->len = txn->req.sl.rq.u_l;
5131 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5132
Willy Tarreauf3d25982007-05-08 22:45:09 +02005133 /* we do not need to set READ_ONLY because the data is in a buffer */
5134 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005135 return 1;
5136}
5137
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005138static int
5139acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5140 struct acl_expr *expr, struct acl_test *test)
5141{
5142 struct http_txn *txn = l7;
5143
5144 if (txn->req.msg_state != HTTP_MSG_BODY)
5145 return 0;
5146 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5147 /* ensure the indexes are not affected */
5148 return 0;
5149
5150 /* Parse HTTP request */
5151 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5152 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5153 test->i = AF_INET;
5154
5155 /*
5156 * If we are parsing url in frontend space, we prepare backend stage
5157 * to not parse again the same url ! optimization lazyness...
5158 */
5159 if (px->options & PR_O_HTTP_PROXY)
5160 l4->flags |= SN_ADDR_SET;
5161
5162 test->flags = ACL_TEST_F_READ_ONLY;
5163 return 1;
5164}
5165
5166static int
5167acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5168 struct acl_expr *expr, struct acl_test *test)
5169{
5170 struct http_txn *txn = l7;
5171
5172 if (txn->req.msg_state != HTTP_MSG_BODY)
5173 return 0;
5174 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5175 /* ensure the indexes are not affected */
5176 return 0;
5177
5178 /* Same optimization as url_ip */
5179 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5180 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5181
5182 if (px->options & PR_O_HTTP_PROXY)
5183 l4->flags |= SN_ADDR_SET;
5184
5185 test->flags = ACL_TEST_F_READ_ONLY;
5186 return 1;
5187}
5188
Willy Tarreauc11416f2007-06-17 16:58:38 +02005189/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5190 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5191 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005192static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005193acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005194 struct acl_expr *expr, struct acl_test *test)
5195{
5196 struct http_txn *txn = l7;
5197 struct hdr_idx *idx = &txn->hdr_idx;
5198 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005199
5200 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5201 /* search for header from the beginning */
5202 ctx->idx = 0;
5203
Willy Tarreau33a7e692007-06-10 19:45:56 +02005204 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5205 test->flags |= ACL_TEST_F_FETCH_MORE;
5206 test->flags |= ACL_TEST_F_VOL_HDR;
5207 test->len = ctx->vlen;
5208 test->ptr = (char *)ctx->line + ctx->val;
5209 return 1;
5210 }
5211
5212 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5213 test->flags |= ACL_TEST_F_VOL_HDR;
5214 return 0;
5215}
5216
Willy Tarreau33a7e692007-06-10 19:45:56 +02005217static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005218acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5219 struct acl_expr *expr, struct acl_test *test)
5220{
5221 struct http_txn *txn = l7;
5222
5223 if (txn->req.msg_state != HTTP_MSG_BODY)
5224 return 0;
5225 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5226 /* ensure the indexes are not affected */
5227 return 0;
5228
5229 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5230}
5231
5232static int
5233acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5234 struct acl_expr *expr, struct acl_test *test)
5235{
5236 struct http_txn *txn = l7;
5237
5238 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5239 return 0;
5240
5241 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5242}
5243
5244/* 6. Check on HTTP header count. The number of occurrences is returned.
5245 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5246 */
5247static int
5248acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005249 struct acl_expr *expr, struct acl_test *test)
5250{
5251 struct http_txn *txn = l7;
5252 struct hdr_idx *idx = &txn->hdr_idx;
5253 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005254 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005255
Willy Tarreau33a7e692007-06-10 19:45:56 +02005256 ctx.idx = 0;
5257 cnt = 0;
5258 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5259 cnt++;
5260
5261 test->i = cnt;
5262 test->flags = ACL_TEST_F_VOL_HDR;
5263 return 1;
5264}
5265
Willy Tarreauc11416f2007-06-17 16:58:38 +02005266static int
5267acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5268 struct acl_expr *expr, struct acl_test *test)
5269{
5270 struct http_txn *txn = l7;
5271
5272 if (txn->req.msg_state != HTTP_MSG_BODY)
5273 return 0;
5274 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5275 /* ensure the indexes are not affected */
5276 return 0;
5277
5278 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5279}
5280
5281static int
5282acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5283 struct acl_expr *expr, struct acl_test *test)
5284{
5285 struct http_txn *txn = l7;
5286
5287 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5288 return 0;
5289
5290 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5291}
5292
Willy Tarreau33a7e692007-06-10 19:45:56 +02005293/* 7. Check on HTTP header's integer value. The integer value is returned.
5294 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005295 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005296 */
5297static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005298acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005299 struct acl_expr *expr, struct acl_test *test)
5300{
5301 struct http_txn *txn = l7;
5302 struct hdr_idx *idx = &txn->hdr_idx;
5303 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005304
5305 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5306 /* search for header from the beginning */
5307 ctx->idx = 0;
5308
Willy Tarreau33a7e692007-06-10 19:45:56 +02005309 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5310 test->flags |= ACL_TEST_F_FETCH_MORE;
5311 test->flags |= ACL_TEST_F_VOL_HDR;
5312 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5313 return 1;
5314 }
5315
5316 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5317 test->flags |= ACL_TEST_F_VOL_HDR;
5318 return 0;
5319}
5320
Willy Tarreauc11416f2007-06-17 16:58:38 +02005321static int
5322acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5323 struct acl_expr *expr, struct acl_test *test)
5324{
5325 struct http_txn *txn = l7;
5326
5327 if (txn->req.msg_state != HTTP_MSG_BODY)
5328 return 0;
5329 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5330 /* ensure the indexes are not affected */
5331 return 0;
5332
5333 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5334}
5335
5336static int
5337acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5338 struct acl_expr *expr, struct acl_test *test)
5339{
5340 struct http_txn *txn = l7;
5341
5342 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5343 return 0;
5344
5345 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5346}
5347
Willy Tarreau737b0c12007-06-10 21:28:46 +02005348/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5349 * the first '/' after the possible hostname, and ends before the possible '?'.
5350 */
5351static int
5352acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5353 struct acl_expr *expr, struct acl_test *test)
5354{
5355 struct http_txn *txn = l7;
5356 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005357
Willy Tarreauc11416f2007-06-17 16:58:38 +02005358 if (txn->req.msg_state != HTTP_MSG_BODY)
5359 return 0;
5360 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5361 /* ensure the indexes are not affected */
5362 return 0;
5363
Willy Tarreau21d2af32008-02-14 20:25:24 +01005364 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5365 ptr = http_get_path(txn);
5366 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005367 return 0;
5368
5369 /* OK, we got the '/' ! */
5370 test->ptr = ptr;
5371
5372 while (ptr < end && *ptr != '?')
5373 ptr++;
5374
5375 test->len = ptr - test->ptr;
5376
5377 /* we do not need to set READ_ONLY because the data is in a buffer */
5378 test->flags = ACL_TEST_F_VOL_1ST;
5379 return 1;
5380}
5381
5382
Willy Tarreau8797c062007-05-07 00:55:35 +02005383
5384/************************************************************************/
5385/* All supported keywords must be declared here. */
5386/************************************************************************/
5387
5388/* Note: must not be declared <const> as its list will be overwritten */
5389static struct acl_kw_list acl_kws = {{ },{
5390 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth },
5391 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str },
5392 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str },
Willy Tarreauae8b7962007-06-09 23:10:04 +02005393 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int },
Willy Tarreau8797c062007-05-07 00:55:35 +02005394
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005395 { "url", acl_parse_str, acl_fetch_url, acl_match_str },
5396 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg },
5397 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end },
5398 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub },
5399 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir },
5400 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom },
5401 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg },
5402 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip },
5403 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int },
Willy Tarreau8797c062007-05-07 00:55:35 +02005404
Willy Tarreauc11416f2007-06-17 16:58:38 +02005405 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str },
5406 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg },
5407 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg },
5408 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end },
5409 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub },
5410 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir },
5411 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom },
5412 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int },
5413 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int },
5414
5415 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str },
5416 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg },
5417 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg },
5418 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end },
5419 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub },
5420 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir },
5421 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom },
5422 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int },
5423 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005424
5425 { "path", acl_parse_str, acl_fetch_path, acl_match_str },
5426 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg },
5427 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg },
5428 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end },
5429 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub },
5430 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir },
5431 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom },
5432
Willy Tarreauf3d25982007-05-08 22:45:09 +02005433 { NULL, NULL, NULL, NULL },
5434
5435#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005436 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5437 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5438 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5439 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5440 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5441 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5442 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5443
Willy Tarreau8797c062007-05-07 00:55:35 +02005444 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5445 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5446 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5447 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5448 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5449 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5450 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5451 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5452
5453 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5454 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5455 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5456 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5457 { NULL, NULL, NULL, NULL },
5458#endif
5459}};
5460
5461
5462__attribute__((constructor))
5463static void __http_protocol_init(void)
5464{
5465 acl_register_keywords(&acl_kws);
5466}
5467
5468
Willy Tarreau58f10d72006-12-04 02:26:12 +01005469/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005470 * Local variables:
5471 * c-indent-level: 8
5472 * c-basic-offset: 8
5473 * End:
5474 */